JavaScript 如何用数组解释索引数组


[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)

为什么这不是一个SyntaxError错误(一个糟糕的遗产或一个有目的的功能(?(可能重复,但我无法在这里找到答案。

更新:为什么the contents of the square brackets are treated as an expression

第一部分:

[1,2,4,8]

被解释为数组文本。第二部分:

[0,1,2,3]

被解释为方括号表示法以访问数组的成员。方括号的内容被视为表达式,它被视为逗号分隔值序列:

0,1,2,3 // or (0,1,2,3) as an independent expression

该表达式返回最后一个值,因此有效地返回:

[1,2,4,8][3] // 8

最新更新