是 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:45:04  来源:igfitidea点击:

is switch(true) {... valid javascript?

javascriptbooleanswitch-statement

提问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 switchin Javascript is very far from the original int-based switchfrom the C language, it only keeps the general semantics.

与许多“现代”语言一样,switchJavascript 中的 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 ifsequence is the ability to ommit the breakstatement 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:

switch 语句语法是:

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. trueis perfectly acceptable as a switch expression and y < 20is 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 truecase will be executed until breakis encountered or the switch block ends.

在您发布的代码中,第一种true情况将一直执行,直到break遇到 或 switch 块结束。

回答by Praveen Kumar Purushothaman

The cases will get executed based on value of y.

这些案例将根据 的值执行y

Because the conditions depend on the value of y. As said by aefxx, it is an other form of:

因为条件取决于 的值y。正如aefxx 所说,它是另一种形式:

if (y < 20) {
    // ...
} elseif (y < 60) {
    // ...
} elseif ( y < 130) {
    // ...
}

回答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 caseis 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 caseexpressions.)

也就是说,在表达式case计算,然后与在表达相比switch。(这与 C 或 Java 等需要case表达式中的常量值的语言截然不同。)

Of course, the breakwill "exit the switch" - as opposed to the standard fall-through semantics - and as such, where trueis the expression supplied to switch, the posted example is semantically equivalent to the if/else ifas shown by aefxx.

当然, the breakwill "exit the switch" - 与标准的 fall-through 语义相反 - 因此,true提供给 的表达式在哪里switch,发布的示例在语义上等同于if/else ifaefxx 所示的 。