为什么00.0会导致语法错误? [英] Why does 00.0 cause a syntax error?

查看:95
本文介绍了为什么00.0会导致语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪。这就是Chrome中的JavaScript控制台(版本42.0.2311.135,64位)所发生的情况。

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

> 0
< 0
> 00
< 0
> 0.0
< 0
> 00.0
X Uncaught > SyntaxError: Unexpected number

Firefox 37.0.2的功能相同,但错误信息是:

Firefox 37.0.2 does the same, although its error message is:

SyntaxError: missing ; before statement

关于JavaScript解析数字的方式可能有一些技术性的解释,也许它只能发生在修改控制台提示,但它似乎仍然是错误的。

There's probably some technical explanation regarding the way JavaScript parses numbers, and perhaps it can only happen when tinkering at the console prompt, but it still seems wrong.

为什么会这样做?

推荐答案

表达式 0.0 00.0 的解析方式不同。

The expressions 0.0 and 00.0 are parsed differently.


  • 0.0 被解析为数字文字 1

  • 00.0 被解析为:

    • 00 - 八进制数字文字 2

    • - 属性访问者

    • 0 - 标识符名称

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

      您的代码会引发语法错误,因为 0 不是有效的JavaScript标识符。以下示例有效,因为 toString 是有效的标识符:

      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
      

      1 第7.8.3节 - 前导0后面可以跟小数点分隔符 ExponentPart

      2 B部分.1.1 - 前导0后面可以跟 OctalDigits

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

      这篇关于为什么00.0会导致语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆