Javascript“元组”符号:它的意义何在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9018295/
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 05:20:30  来源:igfitidea点击:

Javascript "tuple" notation: what is its point?

javascriptsyntaxtuplescomma-operator

提问by Grilse

At wtfjs, I found that the following is legal javascript.

wtfjs,我发现以下是合法的 javascript。

",,," == Array((null,'cool',false,NaN,4)); // true

The argument (null,'cool',false,NaN,4)looks like a tuple to me, but javascript does not have tuples!

参数(null,'cool',false,NaN,4)对我来说看起来像一个元组,但 javascript 没有元组!

Some quick tests in my javascript console yields the following.

我的 javascript 控制台中的一些快速测试产生以下结果。

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?

是否有描述此语法及其语义的参考文献?为什么它存在,即什么时候应该使用它?

回答by Rich O'Kelly

You are seeing the effect of the comma operator.

您正在看到逗号运算符的效果。

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

逗号运算符计算它的两个操作数(从左到右)并返回第二个操作数的值。

The resultant value when a,b,c,...,nis evaluated will always be the value of the rightmost expression, however allexpressions in the chain are still evaluated (from left to right).

a,b,c,...,n求值时的结果值将始终是最右边表达式的值,但仍会求值链中的所有表达式(从左到右)。

回答by dfsq

As already explained this behaviour is caused by ,operator. Due to this the expression (null,'cool',false,NaN,4)will always evaluate to 4. So we have

如前所述,这种行为是由,操作员引起的。因此,表达式(null,'cool',false,NaN,4)将始终计算为4。所以我们有

",,," == Array(4)

Array(4)- creates new array with allocated 4 elements. At the time of comparison with the string this array is converted to string like it would be with Array(4).toString(). For arrays toStringacts like join(',')method called on this array. So for the empty array of 4 elements join will produce the string ",,,".

Array(4)- 创建分配了 4 个元素的新数组。在与字符串进行比较时,此数组将转换为字符串,就像使用Array(4).toString(). 对于数组toString,就像join(',')在这个数组上调用的方法一样。所以对于 4 个元素的空数组,join 将产生字符串",,,"

回答by qwertymk

Try this alert((null,'cool',false,NaN,4))and then you can see.

试试这个alert((null,'cool',false,NaN,4)),然后你就可以看到了。

demo

演示

The reason is because the comma operator evaluates all the statements and return the last one.

原因是逗号运算符计算所有语句并返回最后一个。

Think of this line: a = 1, b = 2, c = 3;it will run each expression so in essence it will set the variables to what you want and return the last value (in this case 3)

想想这一行:a = 1, b = 2, c = 3;它将运行每个表达式,因此本质上它会将变量设置为您想要的值并返回最后一个值(在本例中为 3)