是 switch(true) {... 有效的 javascript 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14118996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is switch(true) {... valid javascript?
提问by PrimeLens
I recently came across code where a switch statement seemed reversed with the answer (boolean) in the switch and the expressions in the case. The code ran fine as intended but I'm concerned about cross browser. Is it valid javascript?
我最近遇到过这样的代码,其中 switch 语句似乎与 switch 中的答案(布尔值)和 case 中的表达式相反。代码按预期运行良好,但我担心跨浏览器。它是有效的javascript吗?
switch(true) {
case (y < 20):
//
break;
case (y < 60):
//
break;
case (y < 130):
//
break;
}
采纳答案by aefxx
This snippet is perfectly fine. It's just another way of expressing:
这个片段非常好。这只是另一种表达方式:
if (y < 20) {
// ...
} else if (y < 60) {
// ...
} else if ( y < 130) {
// ...
}
回答by Denys Séguret
Yes, it's valid.
是的,它是有效的。
As in many "modern" languages, the switch
in Javascript is very far from the original int-based switch
from the C language, it only keeps the general semantics.
与许多“现代”语言一样,switch
Javascript 中的 Javascriptswitch
与 C 语言中的原始 int-based 相去甚远,它只保留了一般语义。
The switch clause, as normalized in ECMAScript, is explained here in details : http://www.ecma-international.org/ecma-262/5.1/#sec-12.11
在 ECMAScript 中规范化的 switch 子句在此处详细解释:http: //www.ecma-international.org/ecma-262/5.1/#sec-12.11
Basically, the first case whose value is equal to the expression in switch(Expression)
is executed.
基本上,其值等于表达式 in 的第一个 caseswitch(Expression)
被执行。
The main advantage over the obvious if else if
sequence is the ability to ommit the break
statement and execute more than one block. Note that, contrary to the old C switch, there is no real performance improvement and in this case it's neither more succinct nor more readable.
明显if else if
序列的主要优点是能够省略break
语句并执行多个块。请注意,与旧的 C 开关相反,没有真正的性能改进,在这种情况下,它既不简洁也不可读。
回答by Salman A
The syntax of switch statementis:
SwitchStatement : switch ( Expression ) CaseBlock CaseBlock : { CaseClauses(opt) } { CaseClauses(opt) DefaultClause CaseClauses(opt) } CaseClauses : CaseClause CaseClauses CaseClause CaseClause : case Expression : StatementList(opt) DefaultClause : default : StatementList(opt)
SwitchStatement : switch ( Expression ) CaseBlock CaseBlock : { CaseClauses(opt) } { CaseClauses(opt) DefaultClause CaseClauses(opt) } CaseClauses : CaseClause CaseClauses CaseClause CaseClause : case Expression : StatementList(opt) DefaultClause : default : StatementList(opt)
No where it says that switch expression or the case expression has to be a number, string, boolean or anything. true
is perfectly acceptable as a switch expression and y < 20
is perfectly acceptable as case expression. Keep in mind that comparison between switch expression and case expressions are made using ===
operator.
没有它说 switch 表达式或 case 表达式必须是数字、字符串、布尔值或任何东西。true
作为 switch 表达式y < 20
是完全可以接受的,作为 case 表达式是完全可以接受的。请记住, switch 表达式和 case 表达式之间的比较是使用===
运算符进行的。
In the code you posted, the first true
case will be executed until break
is encountered or the switch block ends.
在您发布的代码中,第一种true
情况将一直执行,直到break
遇到 或 switch 块结束。
回答by Praveen Kumar Purushothaman
回答by Praveen Kumar Purushothaman
It is valid.
这是有效的。
The code
代码
switch(f0()) {
case f1(): ..;
case f2(): ..;
default: dflt;
}
where fX()
represents an arbitrary expression(a function invocation is used to show forced evaluation) can be approximately re-written as
其中fX()
表示任意表达式(函数调用用于显示强制评估)可以近似重写为
for (;;) { // for "break"
var _x = f0()
if (_x === f1()) { .. }
if (_x === f2()) { .. }
dflt;
break;
}
That is, the expression in the case
is evaluatedand then compared with the expression in the switch
. (This is a sharp divergence from languages like C or Java that require constant values in the case
expressions.)
也就是说,在表达式case
被计算,然后与在表达相比switch
。(这与 C 或 Java 等需要case
表达式中的常量值的语言截然不同。)
Of course, the break
will "exit the switch" - as opposed to the standard fall-through semantics - and as such, where true
is the expression supplied to switch
, the posted example is semantically equivalent to the if/else if
as shown by aefxx.
当然, the break
will "exit the switch" - 与标准的 fall-through 语义相反 - 因此,true
提供给 的表达式在哪里switch
,发布的示例在语义上等同于if/else if
aefxx 所示的 。