如何使用JavaScript在switch case语句中使用范围?因此,我不想为每一种可能性编写代码,而是将它们分组在范围内,例如:
switch(myInterval){
case 0-2:
//doStuffWithFirstRange();
break;
case 3-6:
//doStuffWithSecondRange();
break;
case 6-7:
//doStuffWithThirdRange();
break;
default:
//doStuffWithAllOthers();
}发布于 2013-06-17 19:01:14
您至少有四个选项:
1.列出每个case
作为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();
}根据您在中给出的值,按顺序检查case语句。尽管case是按源代码顺序处理的,但default可以出现在任何地方(而不只是末尾),并且只有在没有匹配的cases或匹配的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映射上调用的函数来实现的,如果没有使用the curiously-powerful || operator的特定myInterval值的条目,则默认为doStuffWithAllOthers,然后调用它。
您可以将其分成两行,以使其更清晰:
var f = dispatcher[myInterval] || doStuffWithAllOthers;
f();为了获得最大的灵活性,我使用了一个对象。您可以使用您的特定示例将dispatcher定义为:
var dispatcher = [
/* 0-2 */
doStuffWithFirstRange,
doStuffWithFirstRange,
doStuffWithFirstRange,
/* 3-5 */
doStuffWithSecondRange,
doStuffWithSecondRange,
doStuffWithSecondRange,
/* 6-7 */
doStuffWithThirdRange,
doStuffWithThirdRange
];...but如果值不是连续的数字,那么使用对象会更清楚。
发布于 2018-02-25 09:26:02
本例中的范围非常小,但这里介绍了如何根据JavaScript MDN Docs处理更大的范围
// The value we'll be evaluating:
let code = 100;
// Matches for any case where the expression === `true`:
switch (true) {
case code <= 64:
return "Your number is 64 or less!";
break;
case code >= 65 && code <= 90:
return "Your number is in the range of 65-90!";
break;
case code >= 97 && code <= 122:
return "Your number is in the range of 97-122!";
break;
case code >= 123:
return "Your number is 123 or greater!";
break;
default:
break;
}我知道T.J. Crowder已经通过Use ExpressionExpression展示了这种风格,但我只是想展示如何使用这种相同方法的另一个示例。我只是这样做了,并认为也许另一个例子可能会对某人有所帮助,因为在看了其他回复后,我仍然有点困惑。
发布于 2013-06-17 18:50:46
这可能就是你需要的吗?
switch(myInterval){
case 0:
case 1:
case 2:
//doStuff();
break;
case 3:
case 4:
case 5:
case 6:
//doStuff();
break;
case 6:
case 7:
//doStuff();
break;
default:
//doStuff();
}如果你知道范围会很大(例如0-100),你也可以这样做,这肯定更容易,更干净,更简单:
if (myInterval >= 0 && myInterval <= 20) {
//doStuff();
} else if (myInterval > 20 && myInterval <= 60) {
//doStuff();
} else if (myInterval > 60 && myInterval <= 70) {
//doStuff();
} else /* it is greater than 70 */ {
//doStuff();
}https://stackoverflow.com/questions/17145723
复制相似问题