[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
为什么这不是 SyntaxError
错误(不良遗产或有目的的功能)? (可能重复,但我无法找到答案 here 。)
更新:为什么 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
关于javascript - JavaScript 如何用数组解释索引数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779840/