Why does 00.0 cause a syntax error?

本小妞迷上赌 提交于 2019-12-02 21:00:19

The expressions 0.0 and 00.0 are parsed differently.

  • 0.0 is parsed as a numeric literal 1
  • 00.0 is parsed as:
    • 00 – octal numeric literal 2
    • . – property accessor
    • 0 – identifier name

Your code throws syntax error because 0 is not a valid JavaScript identifier. The following example works since toString is a valid identifier:

00.toString

1Section 7.8.3 – Leading 0 can be followed by decimal separator or ExponentPart
2Section B.1.1 – Leading 0 can be followed by OctalDigits

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!