简体   繁体   English

Javascript case(true)抛出意外的标识符

[英]Javascript case (true) throwing Unexpected identifier

When I run the code below on Chrome, the console shows an Unexpected identifier exception. 当我在Chrome上运行以下代码时,控制台会显示Unexpected identifier异常。

var a = true;
var b = false;

switch (true) {
  a:
    window.alert('test A');
    break;
  b:
    window.alert('test B');
    break;
  default:
    window.alert('test C');
}

I tried to run the code directly on console to guarantee the error is not caused by other line on my script, but I'm still receiving the exception. 我试图直接在控制台上运行代码,以保证错误不是由我的脚本上的其他行引起的,但我仍然收到异常。

I also looked for an answer through Google, but didn't found any answer for this strange behaviour. 我也通过谷歌寻找答案,但没有找到任何答案这种奇怪的行为。

Thanks. 谢谢。

you can't write b: you must write case b: 你不能写b:你必须写case b:

var a = true;
var b = false;

switch (true) {
  case a:
    window.alert('test A');
    break;
  case b:
    window.alert('test B');
    break;
  default:
    window.alert('test C');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM