Javascript quot;tuplequot; notation: what is its point?(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“元组"符号:它的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript“元组"符号:它的意义是什么?


基础教程推荐
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01