在JavaScript中切换语句多个案例 [英] Switch statement multiple cases in JavaScript
本文介绍了在JavaScript中切换语句多个案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JavaScript中的switch语句中需要多个案例,例如:
I need multiple cases in switch statement in JavaScript, Something like:
switch (varName)
{
case "afshin", "saeed", "larry":
alert('Hey');
break;
default:
alert('Default case');
break;
}
我该怎么做?如果在JavaScript中没有办法做类似的事情,我想知道一个替代解决方案,也遵循 DRY概念。
How can I do that? If there's no way to do something like that in JavaScript, I want to know an alternative solution that also follows DRY concept.
推荐答案
使用开关的防坠落功能
声明。一个匹配的大小写将一直运行,直到找到 break
(或 switch
语句的结尾),所以你可以写它喜欢:
Use the fall-through feature of the switch
statement. A matched case will run until a break
(or the end of the switch
statement) is found, so you could write it like:
switch (varName)
{
case "afshin":
case "saeed":
case "larry":
alert('Hey');
break;
default:
alert('Default case');
}
这篇关于在JavaScript中切换语句多个案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文