鍍金池/ 問答/HTML/ JS Number.toFixed()方法調(diào)用對(duì)象的疑問

JS Number.toFixed()方法調(diào)用對(duì)象的疑問

在babel-node環(huán)境下試了toFixed()方法, 為什么2.toFixed(1)這個(gè)方法會(huì)報(bào)錯(cuò)? 而其他的寫法不會(huì)報(bào)錯(cuò).

Number(2).toFixed(1)
'2.0'
2.00.toFixed(1)
'2.0'
2.0.toFixed(1)
'2.0'
(2).toFixed(2)
'2.00'
2.toFixed(1)
SyntaxError: repl: Identifier directly after number (1:2)
1 | 2.toFixed(1)
回答
編輯回答
涼薄

A common misconception is that number literals cannot be used as objects. That is because a flaw in JavaScript's parser tries to parse the dot notation on a number as a floating point literal.

2017年5月11日 18:50