Javascript 测试开关中的多种情况,例如 OR (||)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6513585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:07:29  来源:igfitidea点击:

Test for multiple cases in a switch, like an OR (||)

javascriptswitch-statementfall-through

提问by Andres

How would you use a switchcasewhen you need to test for aorbin the same case?

switchcase当您需要在同一情况下测试ab时,您将如何使用 a ?

switch (pageid) {
  case "listing-page" || "home-page":
    alert("hello");
    break;
  case "details-page":
    alert("goodbye");
    break;
}

回答by kei

You can use fall-through:

您可以使用跌倒:

switch (pageid)
{
    case "listing-page":
    case "home-page":
        alert("hello");
        break;
    case "details-page":
        alert("goodbye");
        break;
}

回答by nnnnnn

Since the other answers explained how to do it without actually explaining why it works:

由于其他答案解释了如何做到这一点,而没有真正解释它的工作原理:

When the switchexecutes, it finds the first matching casestatement and then executes each line of code after the switch until it hits either a breakstatement or the end of the switch(or a returnstatement to leave the entire containing function). When you deliberately omit the breakso that code under the next casegets executed too that's called a fall-through. So for the OP's requirement:

switch执行时,它找到第一个匹配case语句,然后在切换之后执行的代码的每一行,直至它碰到或者是break语句或的端部switch(或一个return语句留下含整个函数)。当你故意省略 the breaknext 下的代码case也被执行时,这称为fall-through。所以对于OP的要求:

switch (pageid) {
   case "listing-page":
   case "home-page":
      alert("hello");
      break;

   case "details-page":
      alert("goodbye");
      break;
} 

Forgetting to include breakstatements is a fairly common coding mistake and is the first thing you should look for if your switchisn'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:

忘记包含break语句是一个相当常见的编码错误,如果您switch没有按照预期的方式工作,您应该首先寻找它。出于这个原因,有些人喜欢在注释中说“失败”,以明确何时故意省略了 break 语句。我在下面的例子中这样做,因为它有点复杂,并展示了一些案例如何在它们失败之前包含要执行的代码:

switch (someVar) {
   case 1:
      someFunction();
      alert("It was 1");
      // fall through
   case 2:
      alert("The 2 case");
      // fall through
   case 3:
      // fall through
   case 4:
      // fall through
   case 5:
      alert("The 5 case");
      // fall through
   case 6:
      alert("The 6 case");
      break;

   case 7:
      alert("Something else");
      break;

   case 8:
      // fall through
   default:
      alert("The end");
      break;
}

You can also (optionally) include a defaultcase, which will be executed if none of the other cases match - if you don't include a defaultand no cases match then nothing happens. You can (optionally) fall through to the default case.

您还可以(可选)包含一个default案例,如果其他案例都不匹配,则将执行该案例 - 如果您不包含 adefault并且没有案例匹配,则不会发生任何事情。您可以(可选)使用默认情况。

So in my second example if someVaris 1 it would call someFunction()and then you would see four alerts as it falls through multiple cases some of which have alerts under them. Is someVaris 3, 4 or 5 you'd see two alerts. If someVaris 7 you'd see "Something else" and if it is 8 or any other value you'd see "The end".

因此,在我的第二个示例中,如果someVar是 1,它会调用someFunction(),然后您会看到四个警报,因为它遇到了多种情况,其中一些情况下有警报。是someVar3、4 或 5,您会看到两个警报。如果someVar是 7,你会看到“别的东西”,如果是 8 或任何其他值,你会看到“结束”。

回答by SLaks

You need to make two caselabels.

您需要制作两个case标签。

Control will fall through from the first label to the second, so they'll both execute the same code.

控制将从第一个标签传递到第二个标签,因此它们都将执行相同的代码。

回答by Stefano Favero

You have to switch it!

你必须切换它!

switch (true) {
    case ( (pageid === "listing-page") || (pageid === ("home-page") ):
        alert("hello");
        break;
    case (pageid === "details-page"):
        alert("goodbye");
        break;
}

回答by khex

Forget switchand break, lets play with if. And instead of asserting

忘记switchbreak,让我们玩if。而不是断言

if(pageid === "listing-page" || pageid === "home-page")

lets create several arrays with cases and check it with Array.prototype.includes()

让我们用案例创建几个数组并使用Array.prototype.includes()检查它

var caseA = ["listing-page", "home-page"];
var caseB = ["details-page", "case04", "case05"];

if(caseA.includes(pageid)) {
    alert("hello");
}
else if (caseB.includes(pageid)) {
    alert("goodbye");
}
else {
    alert("there is no else case");
}

回答by Dinesh

Use commas to separate case

使用逗号分隔大小写

switch (pageid)
{
    case "listing-page","home-page":
        alert("hello");
        break;
    case "details-page":
        alert("goodbye");
        break;
}