选择报表的开关/如果…其他的

很多次,如果…其他的…有很多分支选项,你会看到:


如果(动物= 'dog){
do'dog ' / /
否则如果}(动物= = =猫){
do'cat ' / /
否则如果}(动物= = =‘'){
do'bird ' / /
否则如果}(动物= = =鱼){
do'fish ' / /
{人}
do'other ' / /
}



它很容易理解,而且很容易写。所有的方式,如果,有太多的选择,所以有人写开关:


Swtich(动物){
case'dog:
do'dog ' / /
打破;
case'cat:
do'cat ' / /
打破;
case'bird:
do'bird ' / /
打破;
case'fish:
do'fish ' / /
打破;
违约:
do'other ' / /
}




但是,仍然可以借鉴使用对象的哈希思想。


功能getanimalname(name){
var动物{ {
狗:函数(){
return'dog;
},
猫:函数(){
return'cat;
},
鸟:函数(){
return'bird;
},
鱼:函数(){
return'fish;
},
默认:函数(){
return'other;
}
};
返回(animals.name | |)(动物。默认);
}
VaR的动物= getanimalname('dog);
console.log(动物); / / 'dog



以上是本文的全部内容,希望大家能喜欢。