问题描述
在 wtfjs,我发现以下是合法的javascript.
At wtfjs, I found that the following is legal javascript.
",,," == Array((null,'cool',false,NaN,4)); // true
参数 (null,'cool',false,NaN,4)
对我来说看起来像一个元组,但 javascript 没有元组!
The argument (null,'cool',false,NaN,4)
looks like a tuple to me, but javascript does not have tuples!
我的 javascript 控制台中的一些快速测试会产生以下结果.
Some quick tests in my javascript console yields the following.
var t = (null,'cool',false,NaN,4); // t = 4
(null,'cool',false,NaN,4) === 4; // true
(alert('hello'), 42); // shows the alert and returns 42
它的行为似乎与分号 ;
分隔的语句列表完全一样,只是返回最后一条语句的值.
It appears to behave exactly like a semicolon ;
separated list of statements, simply returning the value of the last statement.
在某处有描述此语法及其语义的参考吗?为什么会存在,即应该在什么时候使用?
Is there a reference somewhere that describes this syntax and its semantics? Why does it exist, i.e. when should it be used?
推荐答案
你看到了 逗号运算符.
逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值.
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
a,b,c,...,n
求值时的结果值将始终是最右边表达式的值,但是链中的 所有 表达式仍在评估中(从左到右).
The resultant value when a,b,c,...,n
is evaluated will always be the value of the rightmost expression, however all expressions in the chain are still evaluated (from left to right).
这篇关于Javascript“元组"符号:它的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!