我偶然发现了以下 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
语言中标准化?
最佳答案
Only the last result is kept in result of the comma operator , 所以
while (i<4, i<7, i<5, i<6)
真的等同于
while (i<6)
这就是为什么您不经常看到它的原因:如果您的比较没有副作用,它就没有用。
关于javascript - WHILE 条件中多个逗号分隔的表达式的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733155/