本文共 726 字,大约阅读时间需要 2 分钟。
javascript 逗号
I recently discovered a curious yet possibly useful thing in JavaScript, related to the comma operator.
最近,我在JavaScript中发现了一个与逗号运算符有关的奇怪但可能有用的东西。
I mostly use commas to separate properties in an object, or array items.
我通常使用逗号分隔对象或数组项中的属性。
However I never gave much attention the usage of it inside an expression.
但是,我从未在表达式中使用它。
Take this:
拿着这个:
('a', 'b')
Both expressions (in this case strings) are evaluated, and this returns the last element, the expression after the last comma. In this example it returns 'b'
.
这两个表达式(在本例中为字符串)均被求值,这将返回最后一个元素,即最后一个逗号之后的表达式。 在此示例中,它返回'b'
。
You can assign the value to a variable, like this:
您可以将值分配给变量,如下所示:
const letter = ('a', 'b')letter === 'b' //true
翻译自:
javascript 逗号
转载地址:http://damgb.baihongyu.com/