1.toString() SyntaxError in Javascript

后端 未结 4 1376
误落风尘
误落风尘 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-12 00:09

    The . presents ambiguity. Is it a decimal, or a property accessor?

    The interpreter sees it as a decimal, so you can use .. to allow both the decimal, then the property syntax.

    1..toString();
    

    Or use one of the other ways you show to resolve the ambiguity.

提交回复
热议问题