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")

 

posted @ 2022-03-25 16:18  青牛梦旅行  阅读(119)  评论(0)    收藏  举报