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

发布于 2024-11-17 17:49:37 字数 269 浏览 1 评论 0原文

当您需要测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神仙妹妹 2024-11-24 17:49:37

您可以使用失败:

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

You can use fall-through:

switch (pageid)
{
    case "listing-page":
    case "home-page":
        alert("hello");
        break;
    case "details-page":
        alert("goodbye");
        break;
}
紫竹語嫣☆ 2024-11-24 17:49:37

由于其他答案解释了如何做到这一点,但没有实际解释其工作原理:

switch 执行时,它会找到第一个匹配的 case 语句,然后执行之后的每一行代码switch 直到它遇到 break 语句或 switch 的末尾(或 return 语句以离开整个包含函数)。当您故意省略 break 以便下一个 case 下的代码也被执行时,这称为 fall-through。因此,对于OP的要求:

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

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

忘记包含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;
}

您还可以(可选)包含一个 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 matching case statement and then executes each line of code after the switch until it hits either a break statement or the end of the switch (or a return statement to leave the entire containing function). When you deliberately omit the break so that code under the next case gets executed too that's called a fall-through. So for the OP's requirement:

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

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

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

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 default case, which will be executed if none of the other cases match - if you don't include a default 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 call someFunction() and then you would see four alerts as it falls through multiple cases some of which have alerts under them. Is someVar is 3, 4 or 5 you'd see two alerts. If someVar is 7 you'd see "Something else" and if it is 8 or any other value you'd see "The end".

柠檬色的秋千 2024-11-24 17:49:37

你必须切换它!

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

You have to switch it!

switch (true) {
    case ( (pageid === "listing-page") || (pageid === ("home-page") ):
        alert("hello");
        break;
    case (pageid === "details-page"):
        alert("goodbye");
        break;
}
温柔戏命师 2024-11-24 17:49:37

您需要制作两个 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.

倾城°AllureLove 2024-11-24 17:49:37

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

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

创建几个带有大小写的数组,并使用 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");
}

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

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

lets create several arrays with cases and check it with 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");
}
行至春深 2024-11-24 17:49:37

更简洁的方法

Cleaner way to do that ????

if (["listing-page", "home-page"].indexOf(pageid) > -1)
    alert("hello");

else if (["exit-page", "logout-page"].indexOf(pageid) > -1)
    alert("Good bye");

You can do that for multiple values with the same result

蝶…霜飞 2024-11-24 17:49:37

使用逗号分隔大小写

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

Use commas to separate case

switch (pageid)
{
    case "listing-page","home-page":
        alert("hello");
        break;
    case "details-page":
        alert("goodbye");
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文