这是有效的,并且在 JavaScript 中返回数字 1:
++[[]][0]
为什么不报告像 ++[]
这样的错误?
这不是和++[]
一样吗?
注意:这与:Why does ++[[]][+[]]+[+[]] return the string “10”. 有点不同。
我的意思是为什么++[[]][0]
不报告错误? [[]][0]
是变量吗?如果没有,为什么不报告如下错误:
++[] // returns Invalid left-hand side expression in prefix operation.
最佳答案
The prefix increment operator (++
) calls PutValue ,和PutValue throws a ReferenceError when the argument is not a Reference .
所以以同样的方式
var a = [];
++a; // returns 1
有效,因为 a
是对 []
的引用,但 ++[]
无效,因为 []
是数组文字,而不是引用。
同样,[[]][0]
是对 []
的引用,因此增加该引用再次有效。
这也类似于
++1 // throws ReferenceError
var a = 1;
++a // returns 2
关于javascript - 为什么++[[]][0]返回1而不是报错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37837115/