if判断的优化版本(switch、map实现)
方法一(原始):使用if判断进行分流;
方法二(better):switch的case字段进行分流;
方法三(best):使用map集合,通过key-value的值进行相应分流输出。
let da = {
a:"helloAA",
b:"helloBB",
c:"helloCC"
}
// 1、if判断
function goBack(params){
if(params === da.a){
console.log("go to helloAA");
}esle if(params === da.b){
console.log("go to helloBB");
}esle if(params === da.c){
console.log("go to helloCC");
}
}
goBack("helloAA")
// 2、switch方法
function goBack(params){
switch(params){
case da.a:
console.log("go to helloAA");
break;
case da.b:
console.log("go to helloBB");
break;
case da.c:
console.log("go to helloCC");
break;
}
}
goBack("helloAA")
// 3、map集合
const da2 = new Map([
["helloAA","go to helloAA"],
["helloBB","go to helloBB"],
["helloCC","go to helloCC"]
])
function goBack(params){
return da2.get(params)
}
goBack("helloAA")
人生很长,一秒太短,一个月不长不短刚刚好! 加油少年! ---------LeaningBD
本文来自博客园,作者:青牛梦旅行,转载请注明原文链接:https://www.cnblogs.com/mengxiangtiankongfenwailan/p/16055258.html

if判断走不同分支的代码优化
浙公网安备 33010602011771号