当尝试访问对象 {}
的属性 a
时
{}.a
我得到了错误
SyntaxError: Unexpected token .
有了 parent ,一切都很好:
({}).a
为什么我会在第一个地方出错?有歧义吗?
最佳答案
大括号被解释为 block statement ,而不是作为对象文字。不能以左花括号开始表达式语句。
规范指出:
NOTE An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block. Also, an ExpressionStatement cannot start with the
function
keyword because that might make it ambiguous with a FunctionDeclaration.
关于javascript - 为什么直接在对象文字上访问属性会抛出 SyntaxError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8553792/