Javascript“元组”符号:它有什么意义? [英] Javascript "tuple" notation: what is its point?

查看:134
本文介绍了Javascript“元组”符号:它有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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“元组”符号:它有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆