1.toString() SyntaxError in Javascript

后端 未结 4 1383
误落风尘
误落风尘 2020-12-11 23:43

Why the first line below gives error although the second and third lines work fine?

1.toString(); // SyntaxError
(1).toString(); // OK
1[\'toString\'](); //          


        
4条回答
  •  难免孤独
    2020-12-11 23:57

    The parser is trying to treat 1. as the start of a floating-point literal -- only toString turns it into an invalid number.

    Compare with:

    1.0.toString()
    

提交回复
热议问题