如何使用JavaScript在switch case语句中使用范围? [英] How can I use ranges in a switch case statement using JavaScript?
问题描述
如何使用JavaScript在switch case语句中使用范围?因此,我不想为每种可能性编写代码,而是将它们分组到范围中,例如:
How can I use ranges in a switch case statement using JavaScript? So, instead of writing code for each and every single possibility, I'd like to group them in ranges, For example:
switch(myInterval){
case 0-2:
//doStuffWithFirstRange();
break;
case 3-6:
//doStuffWithSecondRange();
break;
case 6-7:
//doStuffWithThirdRange();
break;
default:
//doStuffWithAllOthers();
}
推荐答案
您至少有四个选项:
作为 LightStyle显示,您可以明确列出每个案例:
As shown by LightStyle, you can list each case explicitly:
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 if
/ else
如果范围很大,那就太笨重了,所以你想做范围。请注意,使用 if ... else if ... else if
,如果之前的匹配,则不会使用后者,因此您只需指定每次上限。为了清楚起见,我将在 /*...*/
中包含下限,但通常您会将其保留为避免引入维护问题(如果包含两个边界) ,很容易改变一个而忘记改变另一个):
2. Use if
/ else if
/ else
If the ranges are large, that gets unwieldy, so you'd want to do ranges. Note that with if...else if...else if
, you don't get to the later ones if an earlier one matches, so you only have to specify the upper bound each time. I'll include the lower bound in /*...*/
for clarity, but normally you would leave it off to avoid introducing a maintenance issue (if you include both boundaries, it's easy to change one and forget to change the other):
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,如果
上面的序列作为开关
声明:
3. Use case
with expressions:
JavaScript is unusual in that you can use expressions in the case
statement, so we can write the if...else if...else if
sequence above as a switch
statement:
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
s-代码顺序,默认
可以出现在任何地方(不仅仅是在结尾),只有在没有 case
s时才会处理匹配或案例
匹配并跌至默认值(没有休息
;您很少想要这样做,但它发生了。)
I'm not advocating that, but it is an option in JavaScript, and there are times it's useful. The case
statements are checked in order against the value you give in the switch
. (And again, lower bounds could be omitted in many cases because they would have matched earlier.) Even though the case
s are processed in source-code order, the default
can appear anywhere (not just at the end) and is only processed if either no case
s matched or a case
matched and fell through to the default (didn't have a break
; it's rare you want to do that, but it happens).
如果你的函数都采用相同的参数(并且可能没有参数,或者只是相同的参数),另一种方法是调度图:
If your functions all take the same arguments (and that could be no arguments, or just the same ones), another approach is a dispatch map:
在某些设置代码中:
var dispatcher = {
0: doStuffWithFirstRange,
1: doStuffWithFirstRange,
2: doStuffWithFirstRange,
3: doStuffWithSecondRange,
4: doStuffWithSecondRange,
5: doStuffWithSecondRange,
6: doStuffWithThirdRange,
7: doStuffWithThirdRange
};
然后代替开关:
(dispatcher[myInterval] || doStuffWithAllOthers)();
通过查找函数来调用调度程序
map,默认为 doStuffWithAllOthers
如果使用 myInterval 值的条目http://blog.niftysnippets.org/2008/02/javascripts-curiously-powerful-or.htmlrel =noreferrer>奇特强大的 ||
运算符,然后调用它。
That works by looking up the function to call on the dispatcher
map, defaulting to doStuffWithAllOthers
if there's no entry for that specific myInterval
value using the curiously-powerful ||
operator, and then calling it.
您可以将其分成两行以使其更清晰:
You can break that into two lines to make it a bit clearer:
var f = dispatcher[myInterval] || doStuffWithAllOthers;
f();
我使用了一个对象以获得最大的灵活性。您可以使用您的具体示例 定义调度程序
:
I've used an object for maximum flexibility. You could define dispatcher
like this with your specific example:
var dispatcher = [
/* 0-2 */
doStuffWithFirstRange,
doStuffWithFirstRange,
doStuffWithFirstRange,
/* 3-5 */
doStuffWithSecondRange,
doStuffWithSecondRange,
doStuffWithSecondRange,
/* 6-7 */
doStuffWithThirdRange,
doStuffWithThirdRange
];
...但如果值不是连续数字,则使用对象更清楚。
...but if the values aren't contiguous numbers, it's much clearer to use an object instead.
这篇关于如何使用JavaScript在switch case语句中使用范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!