Why does 00.0 cause a syntax error?

后端 未结 2 380
旧时难觅i
旧时难觅i 2021-02-02 05:10

This is weird. This is what happens at the JavaScript console in Chrome (version 42.0.2311.135, 64-bit).

> 0
< 0
         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 05:38

    00 is evaluated as an octal number and .0 is evaluated as accessing that number's property. But since integers are not allowed to be used as property accessors, the error is thrown.

    You get the same error for any other object:

    'string'.0 // Syntax error: unexpected number
    ({}).0 // Syntax error: unexpected number
    

    You can find related information about property accessors on MDN.

提交回复
热议问题