1.switch
2.3元表示式
3.用对象处理
let statusObj = { "hello": "您好", "world": "world" } let status = "hello"; console.log(statusObj[status]); // 等同 let string = ""; if (status == "hello") { string = "您好"; } else if (status == "world") { string = "world"; } console.log(string);
4.使用map处理
var actions = new Map([ [1, ['processing','IndexPage']], [2, ['fail','FailPage']], [3, ['fail','FailPage']], [4, ['success','SuccessPage']], [5, ['cancel','CancelPage']], ['default', ['other','Index']] ]) let action = actions.get(1)|| actions.get('default'); console.log(action[0]); console.log(action[1]);
注:尽量把可提取的东西提取出来,减少重复性的代码