javascript - 如何使用 JavaScript 在 switch case 语句中使用范围?

标签 javascript switch-statement intervals

如何使用 JavaScript 在 switch case 语句中使用范围?因此,我不想为每一种可能性编写代码,而是想将它们分组在范围内,例如:

switch(myInterval){
   case 0-2:
      //doStuffWithFirstRange();
      break;

   case 3-6:
      //doStuffWithSecondRange();
      break;

   case 6-7:
      //doStuffWithThirdRange();
      break;

   default:
      //doStuffWithAllOthers();
}

最佳答案

您至少有四种选择:

1。列出每个案例

作为shown by LightStyle ,您可以明确列出每个案例:

switch(myInterval){

    case 0:
    case 1:
    case 2:
        doStuffWithFirstRange();
        break;

    case 3:
    case 4:
    case 5:
        doStuffWithSecondRange();
        break;

    case 6:
    case 7:
        doStuffWithThirdRange();
        break;

    default:
        doStuffWithAllOthers();
}

2。使用 if/else if/else

如果范围很大,那会变得很笨重,所以你会想要做范围。请注意,使用 if...else if...else if 时,如果较早的匹配,则不会进入较晚的匹配,因此每次只需指定上限。为清楚起见,我将在 /*...*/ 中包含下限,但通常您会保留它以避免引入维护问题(如果您包含两个边界,则很容易更改一个忘记改变另一个):

if (myInterval < 0) {
    // I'm guessing this is an error
}
else if (/* myInterval >= 0 && */ myInterval <= 2){
    doStuffWithFirstRange();
}
else if (/* myInterval >= 3 && */ myInterval <= 5) {
    doStuffWithSecondRange();
}
else if (/* myInterval >= 6 && */ myInterval <= 7) {
    doStuffWithThirdRange();
}
else {
    doStuffWithAllOthers();
}

3。将 case 与表达式一起使用:

JavaScript 的不寻常之处在于您可以在 case 语句中使用表达式,因此我们可以将上面的 if...else if...else if 序列写为switch 语句:

switch (true){

    case myInterval < 0:
        // I'm guessing this is an error
        break;    
    case /* myInterval >= 0 && */ myInterval <= 2:
        doStuffWithFirstRange();
        break;

    case /* myInterval >= 3 && */ myInterval <= 5:
        doStuffWithSecondRange();
        break;

    case /* myInterval >= 6 && */ myInterval <= 7:
        doStuffWithThirdRange();
        break;

    default:
        doStuffWithAllOthers();
}

我不是提倡那样做,但它 JavaScript 中的一个选项,有时它很有用。 case 语句根据您在 switch 中提供的值按顺序进行检查。 (同样,在许多情况下可以省略下限,因为它们会更早匹配。)即使 case 是按源代码顺序处理的,default 可以出现在任何地方(不仅仅是最后)并且只有在没有 case 匹配或 case 匹配并落入默认值时才会被处理(没有 break;您很少想这样做,但它确实发生了)。

4。使用调度图

如果您的函数都采用相同的参数(并且可以没有参数,或者只是相同的参数),另一种方法是调度映射:

在一些设置代码中:

var dispatcher = {
    0: doStuffWithFirstRange,
    1: doStuffWithFirstRange,
    2: doStuffWithFirstRange,

    3: doStuffWithSecondRange,
    4: doStuffWithSecondRange,
    5: doStuffWithSecondRange,

    6: doStuffWithThirdRange,
    7: doStuffWithThirdRange
};

然后代替开关:

(dispatcher[myInterval] || doStuffWithAllOthers)();

这是通过查找要在 dispatcher 映射上调用的函数来实现的,如果没有针对该特定 myInterval 值的条目,则默认为 doStuffWithAllOthers使用 the curiously-powerful || operator , 然后调用它。

您可以将其分成两行以使其更加清晰:

var f = dispatcher[myInterval] || doStuffWithAllOthers;
f();

我使用了一个对象以获得最大的灵 active 。您可以用您的特定示例定义 dispatcher:

var dispatcher = [
    /* 0-2 */
    doStuffWithFirstRange,
    doStuffWithFirstRange,
    doStuffWithFirstRange,

    /* 3-5 */
    doStuffWithSecondRange,
    doStuffWithSecondRange,
    doStuffWithSecondRange,

    /* 6-7 */
    doStuffWithThirdRange,
    doStuffWithThirdRange
];

...但如果值不是连续的数字,则使用对象会更清楚。

关于javascript - 如何使用 JavaScript 在 switch case 语句中使用范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145723/

相关文章:

java - 让 switch case 执行之前的 case

javascript - 调用从 00m :00s after certain time 开始的定时器

java - 有没有一种简单的方法可以在 Java 中计算和格式化时间/日期间隔?

Mysql timestampdiff 重叠间隔之间的总和

javascript - 如何使用 jQuery 将事件附加到动态 HTML 元素?

javascript - 如何制作进度条动画

javascript - jQuery:价格不会更新 - 它不会正确减去(使用 switch 语句)

javascript - React 和 Redux 中的事件监听器内存泄漏

javascript - 如何使用 javascript/jquery 从字符串中删除文本?

java - Java 1.8 中的 switch-case 与 Eclipse 不可能吗?