做者:Valentino Gagliardi
译者:前端小智
来源:valentinog
点赞再看,微信搜索
【大迁世界】 关注这个没有大厂背景,但有着一股向上积极心态人。本文
GitHub
https://github.com/qq44924588... 上已经收录,文章的已分类,也整理了不少个人文档,和教程资料。
switch
很方便:给定一个表达式,咱们能够检查它是否与一堆case
子句中的其余表达式匹配。 考虑如下示例:javascript
const name = "Juliana"; switch (name) { case "Juliana": console.log("She's Juliana"); break; case "Tom": console.log("She's not Juliana"); break; }
当 name
为“Juliana”时,咱们将打印一条消息,并当即中断退出该块。 在switch
函数内部时,直接在 case 块使用 return
,就能够省略break
。前端
当没有匹配项时,能够使用 default
选项:java
const name = "Kris"; switch (name) { case "Juliana": console.log("She's Juliana"); break; case "Tom": console.log("She's not Juliana"); break; default: console.log("Sorry, no match"); }
switch
在 Redux reducers 中也大量使用(尽管Redux Toolkit简化了样板),以免产生大量的if
。 考虑如下示例:node
const LOGIN_SUCCESS = "LOGIN_SUCCESS"; const LOGIN_FAILED = "LOGIN_FAILED"; const authState = { token: "", error: "", }; function authReducer(state = authState, action) { switch (action.type) { case LOGIN_SUCCESS: return { ...state, token: action.payload }; case LOGIN_FAILED: return { ...state, error: action.payload }; default: return state; } }
这有什么问题吗?几乎没有。可是有没有更好的选择呢?git
来自 Telmo 的这条 Tweet引发了个人注意。 他展现了两种“switch”
风格,其中一种很是接近Python中的模式。github
Python 没有开关,它给咱们一个更好的替代方法。 首先让咱们将代码从 JavaScript 移植到Python:安全
LOGIN_SUCCESS = "LOGIN_SUCCESS" LOGIN_FAILED = "LOGIN_FAILED" auth_state = {"token": "", "error": ""} def auth_reducer(state=auth_state, action={}): mapping = { LOGIN_SUCCESS: {**state, "token": action["payload"]}, LOGIN_FAILED: {**state, "error": action["payload"]}, } return mapping.get(action["type"], state)
在 Python 中,咱们能够使用字典来模拟switch
。 dict.get()
能够用来表示 switch
的 default
语句。微信
当访问不存在的key
时,Python 会触发一个 KeyError
错误:app
>>> my_dict = { "name": "John", "city": "Rome", "age": 44 } >>> my_dict["not_here"] # Output: KeyError: 'not_here'
.get()
方法是一种更安全方法,由于它不会引起错误,而且能够为不存在的key
指定默认值:函数
>>> my_dict = { "name": "John", "city": "Rome", "age": 44 } >>> my_dict.get("not_here", "not found") # Output: 'not found'
所以,Pytho n中的这一行:
return mapping.get(action["type"], state)
等价于 JavaScript中的:
function authReducer(state = authState, action) { ... default: return state; ... }
再次思考前面的示例:
const LOGIN_SUCCESS = "LOGIN_SUCCESS"; const LOGIN_FAILED = "LOGIN_FAILED"; const authState = { token: "", error: "", }; function authReducer(state = authState, action) { switch (action.type) { case LOGIN_SUCCESS: return { ...state, token: action.payload }; case LOGIN_FAILED: return { ...state, error: action.payload }; default: return state; } }
若是不使用 switch
咱们能够这样作:
function authReducer(state = authState, action) { const mapping = { [LOGIN_SUCCESS]: { ...state, token: action.payload }, [LOGIN_FAILED]: { ...state, error: action.payload } }; return mapping[action.type] || state; }
这里咱们使用 ES6 中的计算属性,此处,mapping
的属性是根据两个常量即时计算的:LOGIN_SUCCESS
和 LOGIN_FAILED
。
属性对应的值,咱们这里使用的是对象解构,这里 ES9((ECMAScript 2018)) 出来的。
const mapping = { [LOGIN_SUCCESS]: { ...state, token: action.payload }, [LOGIN_FAILED]: { ...state, error: action.payload } }
你如何看待这种方法?它对 switch
来讲可能还能一些限制,但对于 reducer 来讲多是一种更好的方案。
可是,此代码的性能如何?
switch
的性能优于字典
的写法。咱们能够使用下面的事例测试一下:
console.time("sample"); for (let i = 0; i < 2000000; i++) { const nextState = authReducer(authState, { type: LOGIN_SUCCESS, payload: "some_token" }); } console.timeEnd("sample");
测量它们十次左右,
for t in {1..10}; do node switch.js >> switch.txt;done for t in {1..10}; do node map.js >> map.txt;done
人才们的 【三连】 就是小智不断分享的最大动力,若是本篇博客有任何错误和建议,欢迎人才们留言,最后,谢谢你们的观看。
原文:https://codeburst.io/alternat...
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
文章每周持续更新,能够微信搜索 【大迁世界 】 第一时间阅读,回复 【福利】 有多份前端视频等着你,本文 GitHub https://github.com/qq449245884/xiaozhi 已经收录,欢迎Star。