Vid*_*dul 0 javascript arrays indexing
[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
Run Code Online (Sandbox Code Playgroud)
为什么这不是SyntaxError错误(糟糕的遗产或有目的的功能)?(可能重复,但我无法在这里找到答案。)
更新:为什么the contents of the square brackets are treated as an expression?
第一部分:
[1,2,4,8]
Run Code Online (Sandbox Code Playgroud)
被解释为数组文字。第二部分:
[0,1,2,3]
Run Code Online (Sandbox Code Playgroud)
被解释为方括号表示法来访问数组的成员。方括号的内容被视为一个表达式,它被视为一个逗号分隔值的序列:
0,1,2,3 // or (0,1,2,3) as an independent expression
Run Code Online (Sandbox Code Playgroud)
该表达式返回最后一个值,因此有效:
[1,2,4,8][3] // 8
Run Code Online (Sandbox Code Playgroud)