有人能详细解释一下这里发生了什么吗?特别是双点符号。
(3.14).toFixed(); // "3"
3.14.toFixed(); // "3"
(3).toFixed(); // "3"
3.toFixed(); // SyntaxError: Unexpected token ILLEGAL
3..toFixed(); // "3"
有人能详细解释一下这里发生了什么吗?特别是双点符号。
(3.14).toFixed(); // "3"
3.14.toFixed(); // "3"
(3).toFixed(); // "3"
3.toFixed(); // SyntaxError: Unexpected token ILLEGAL
3..toFixed(); // "3"
DecimalLiteral ::
DecimalIntegerLiteral . [DecimalDigits] [ExponentPart]
. DecimalDigits [ExponentPart]
DecimalIntegerLiteral [ExponentPart]
注意:方括号仅表示这些部分是可选的。
因此,当您说
3.toFixed()
在消耗了3.
之后,解析器认为当前的令牌是十进制字面量的一部分,但它只能被DecimalDigits
或ExponentPart
跟随。但它发现了无效的t
,因此会失败并报错SyntaxError。
当您执行时
3..toFixed()
3.
之后,它看到了被称为属性访问运算符的.
。因此,它省略了可选的DecimalDigits
和ExponentPart
,构造了一个浮点对象,并继续调用toFixed()
方法。3 .toFixed()
3.
是一个数字,所以 .
是小数点,不会开始一个属性。
3..something
是一个数字后面跟着一个属性。