1、简单示例如下:
switch用例
var caseContent = '';//条件判断后处理内容
var caseValue = 5 ;//条件判断值
switch(caseValue){
case 0:
caseContent = "鞋子";
break;
case 1:
caseContent = "裤子";
break;
case 2:
caseContent = "外套";
break;
... ...
case 5:
caseContent = "帽子";
break;
default :
caseContent = "随便";
break;
}
数组用例
var caseContent = '';//条件判断后处理内容
var caseValue = 5 ;//条件判断值
var caseContentArr = ["鞋子","裤子","外套"... ...,"帽子"];
caseContent = caseContentArr[caseValue] ? caseContentArr[caseValue]:"随便";
2、优缺点分析
数组方式代码简洁、效率高,但可读性不如switch用例。