一开始的写法:
switch (type) {
case 1:
console.log("aaa");
break;
case (2||3||4||5||6||7):
console.log("aaa");
break;
case 8:
console.log("aaa");
break;
default:
break;
}
悲催的是,上面这种写法只有当type为2时才能case到,后面的3/4/5/6/7都case不到了,555~
正确写法如下:
switch (type) {
case 1:
console.log("aaa");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
console.log("aaa");
break;
case 8:
console.log("aaa");
break;
default:
break;
}
参考:
Multiple conditions in single case in Switch in JavaScript