0
当对象文字调用toString()
像{}.toString()
这样的方法将导致语法错误,但是当数组文字调用toString()
它是确定的。而当我将对象文字赋值给一个变量时,那么当它调用toString()
方法时就OK了。为什么?例如:为什么不能像{} .toString()这样的对象直接调用toString()方法导致错误?
var o = {};
o.toString(); // OK
{}.toString();
// > Uncaught SyntaxError: Unexpected token .
[1, 2, 3].toString(); // OK
谢谢!
好答案!你能否详细解释它,为什么会发生这种情况?谢谢。 – zhenguoli
[分组](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping)具有较高的[优先级](https://developer.mozilla.org/en/ docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table)而不是[成员访问](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#Dot_notation)。 –