测试开关中的多种情况,例如 OR (||)
当您需要测试 a 或 bswitch case
> 在同样的情况下?
switch (pageid) {
case "listing-page":
case "home-page":
alert("hello");
break;
case "details-page":
alert("goodbye");
break;
}
How would you use a switch
case
when you need to test for a or b in the same case?
switch (pageid) {
case "listing-page":
case "home-page":
alert("hello");
break;
case "details-page":
alert("goodbye");
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用失败:
You can use fall-through:
由于其他答案解释了如何做到这一点,但没有实际解释其工作原理:
当
switch
执行时,它会找到第一个匹配的case
语句,然后执行之后的每一行代码switch 直到它遇到break
语句或switch
的末尾(或return
语句以离开整个包含函数)。当您故意省略break
以便下一个case
下的代码也被执行时,这称为 fall-through。因此,对于OP的要求:忘记包含
break
语句是一个相当常见的编码错误,如果您的switch
没有按照您的方式工作,那么您应该首先查找预期的。出于这个原因,有些人喜欢添加注释来表示“失败”,以明确何时故意省略了break语句。我在下面的示例中这样做,因为它有点复杂,并展示了某些情况如何包含在失败之前执行的代码:您还可以(可选)包含一个
default
情况,它将如果没有其他情况匹配,则执行 - 如果您不包含default
并且没有情况匹配,则不会发生任何情况。您可以(可选)进入默认情况。因此,在我的第二个示例中,如果
someVar
为 1,它将调用someFunction()
,然后您会看到四个警报,因为它经历了多种情况,其中一些情况下有警报。如果someVar
是 3、4 或 5,您会看到两个警报。如果someVar
是 7,您会看到“Something else”,如果是 8 或任何其他值,您会看到“The end”。Since the other answers explained how to do it without actually explaining why it works:
When the
switch
executes, it finds the first matchingcase
statement and then executes each line of code after the switch until it hits either abreak
statement or the end of theswitch
(or areturn
statement to leave the entire containing function). When you deliberately omit thebreak
so that code under the nextcase
gets executed too that's called a fall-through. So for the OP's requirement:Forgetting to include
break
statements is a fairly common coding mistake and is the first thing you should look for if yourswitch
isn't working the way you expected. For that reason some people like to put a comment in to say "fall through" to make it clear when break statements have been omitted on purpose. I do that in the following example since it is a bit more complicated and shows how some cases can include code to execute before they fall-through:You can also (optionally) include a
default
case, which will be executed if none of the other cases match - if you don't include adefault
and no cases match then nothing happens. You can (optionally) fall through to the default case.So in my second example if
someVar
is 1 it would callsomeFunction()
and then you would see four alerts as it falls through multiple cases some of which have alerts under them. IssomeVar
is 3, 4 or 5 you'd see two alerts. IfsomeVar
is 7 you'd see "Something else" and if it is 8 or any other value you'd see "The end".你必须切换它!
You have to switch it!
您需要制作两个
case
标签。控制将从第一个标签传递到第二个标签,因此它们都将执行相同的代码。
You need to make two
case
labels.Control will fall through from the first label to the second, so they'll both execute the same code.
忘记
switch
和break
,让我们玩一下if
。我们不用断言,而是创建几个带有大小写的数组,并使用 Array.prototype.includes()
Forget
switch
andbreak
, lets play withif
. And instead of assertinglets create several arrays with cases and check it with Array.prototype.includes()
更简洁的方法
Cleaner way to do that ????
You can do that for multiple values with the same result
使用逗号分隔大小写
Use commas to separate case