使用object literal替换switch

提问:

1.为什么要使用switch方法  ==> (替换冗长的if和else判断)

2.什么场景下使用 ==> (在判断布尔值的)

3.switch有什么优点 ==> (简化了代码,语法更清晰)

4.switch有什么缺点 ==> (太多的case和break关键字,出现bug时难于调试)

5.还有什么更好的替换方法吗  ==> (使用对象字面量)

6.object literal有什么优点 ==> (可扩展性,可维护性,和更好的调试);

1.switch语法(替换冗长的if和else判断)

(function(){
    var type = "coke";
    var drink;
    switch(type){
        case "coke" :
            drink = "coke";
            break;
        case "pepsi":
            drink = "pepsi";
            break;
        default :
            drink = "unknown drink"
    }
    console.log(drink);
}());

2.if和else做太多判断,语句太冗长

(function(){
    var type = "coke";
    if(type === "coke"){
        type = "coke";
    }else if(type === "pepsi"){
        type = "pepsi";
    }else{
        type = "Unknown drink";
    }
}());

3.使用对象字面量

(function(){
    function getDrink(type){
        var drinks = {
            "coke" : "Coke",
            "pepsi" : "Pepsi",
            "lemoande" : "Lemonade",
            "default" : "Default item"
        };
        return "The drink i chose was " + (drinks[type] || drinks["default"]);
    }
    var drink = getDrink("pepsi");      //有选择参数
    var drink2 = getDrink();            //默认
    console.log(drink);
    console.log(drink2);
}());

4.返回函数调用

(function(){
    var type = "coke";
    var drinks = {
        coke : function(){
            return "Coke";
        },
        pepsi : function(){
            return "Pepsi";
        },
        lemoande : function(){
            return "Lemoande";
        }
    }
    console.log(drinks[type]());
}());

5.返回有“默认值”函数调用

(function(){
    function getDrink(type){
        var fn;
        var drinks = {
            coke : function(){
                return "Coke";
            },
            pepsi : function(){
                return "Pepsi";
            },
            lemoande : function(){
                return "Lemoande";
            },
            default : function(){
                return "Default item";
            }
        };
        if(drinks[type]){
            fn = drinks[type];
        }else{
            fn = drinks["default"];
        }
        return fn();
    }
    var drink = getDrink("lemoande");
    var drink2 = getDrink();
    console.log(drink);     //Lemoande
    console.log(drink2);    //Default item
}());

6.使用||表达式来返回“默认值”函数调用

(function(){
    function getDrink(type){
        var drinks = {
            coke : function(){
                return "Coke";
            },
            pepsi : function(){
                return "Pepsi";
            },
            lemoande : function(){
                return "Lemoande";
            },
            default : function(){
                return "Default item";
            }
        };
        return (drinks[type] || drinks["default"])();
    }
    var drink = getDrink("lemoande");
    var drink2 = getDrink();
    console.log(drink);     //Lemoande
    console.log(drink2);    //Default item
}());

7.有其他返回值的时候

(function(){
    function getDrink(type){
        var item;
        var drinks = {
            coke : function(){
                item = "Coke";
            },
            pepsi : function(){
                item = "Pepsi";
            },
            lemoande : function(){
                item = "Lemoande";
            },
            default : function(){
                item = "Default item";
            }
        };

        //invoke it
        (drinks[type] || drinks["default"])();

        //return something
        return 'The drink I chose was ' + item;
    }

    var drink = getDrink("lemoande");
    var drink2 = getDrink();
    console.log(drink);     //Lemoande
    console.log(drink2);    //Default item
}());

8.当有多个case同一个条件的switch

(function(){
    var type = "coke";
    var snack;
    switch(type){
        case "coke" :
        case "pepsi" :
            snack = "Drink";
            break;
        case "cookies" :
        case "crisps" :
            snack = "Food";
            break;
        default :
            snack = "Unknown type!";
    }
    console.log(snack);
}());

9.使用对象直接量替换switch

(function(){
    function getSnack(type){
        var snack;
        function isDrink(){
            return snack = "Drink";
        }
        function isFood(){
            return snack = "Food";
        }
        var snacks = {
            "coke" : isDrink,
            "pepsi" : isDrink,
            "cookies" : isFood,
            "crisps" : isFood
        };
        return snacks[type]();
    }
    var drink = getSnack("coke");
    console.log(drink);
}());

10.总结

1.switch有太多case和break关键字,且太散乱,出现bug时难于调试。

2.对象字面量有更多的可扩展性,可维护性,和更好的调试,可以包含函数和任何其他对象类型,非常有灵活性,且可以产生闭包,返回闭包。

PS:原文参考:https://toddmotto.com/deprecating-the-switch-statement-for-object-literals/

原文地址:https://www.cnblogs.com/alantao/p/5793442.html