测试开关中的多个案例,例如 OR (||)

当你需要测试 a or b 时,你会如何使用 switch case 同样的情况?

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" || "home-page":
    alert("hello");
    break;
  case "details-page":
    alert("goodbye");
    break;
}

推荐答案

你可以使用fall-through:

You can use fall-through:

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

相关文章