Why is 0[0] syntactically valid?

后端 未结 7 692
天涯浪人
天涯浪人 2020-12-04 13:48

Why is this line valid in javascript ?

var a = 0[0];

After that, a is undefined.

7条回答
  •  半阙折子戏
    2020-12-04 14:37

    There are situations where you could validly subscript a number in Javascript:

    -> 0['toString']
    function toString() { [native code] }
    

    While not immediately apparent why you would want to do this, subscripting in Javascript is equivalent to using dotted notation (albeit the dot notation limits you to using identifiers as keys).

提交回复
热议问题