why can't object literal call toString() method like {}.toString() cause error?

后端 未结 1 787
粉色の甜心
粉色の甜心 2021-01-20 15:35

When object literal calls toString() method like {}.toString() will cause syntax error, but when array literal call toString() it is O

相关标签:
1条回答
  • 2021-01-20 16:09

    It's because {} is seen as a valid block first, instead of a literal in that context.

    In simple terms - consider that line is interpreted from left to right, encounters { and therefore expects that a block has started. When the block ends, it encounters . and that identifier is not allowed there.

    If you were to use ({}).toString() that will work.

    This is because the line starts with ( and therefore expects an expression, it correctly identifies the {} as an expression which is evaluated to an empty object and therefore '.toString()` is allowed.

    If {} is used in another context - e.g. in your example o = {} this is correctly interpreted as an empty object because is's on the right-hand side of an assignment (after =).

    Note that in ES6 there is a similar but more common/practical situation where understanding this is important - when returning objects in one-liner arrow functions, e.g.

    [1,2,3,4].map(val => { v: val, isOdd: v % 2 === 1 }) // ERROR
    [1,2,3,4].map(val => ({ v: val, isOdd: v % 2 === 1 })) // OK
    
    0 讨论(0)
提交回复
热议问题