【发布时间】:2014-02-12 16:04:26
【问题描述】:
我偶然发现了以下 JavaScript:
代码:
var x="", i=0;
while (i<4, i<7, i<5, i<6)
{
x=x + "The number is " + i + "<br>";
i++;
}
document.write(x);
我从来不知道可以在一个WHILE 语句中使用多个逗号分隔的表达式。
结果:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
这是什么原因以及行为是什么(只有最后一个表达式被评估为 while-exit)?
所有Javascript/C/Java 语言的行为是否标准化?
【问题讨论】:
-
以这种方式编写代码没有充分的理由。
-
这里只使用最后一个(ijavascriptweblog.wordpress.com/2011/04/04/…
-
好的...如果您知道它被称为“逗号运算符”,这似乎是一个愚蠢的问题:-) 谢谢大家:-)
-
这看起来不像一个愚蠢的问题,只是一个你应该避免的愚蠢代码。我希望这不是一种教程。
标签: javascript syntax coding-style