Why can't I access a property of an integer with a single dot?

前端 未结 4 1832
醉话见心
醉话见心 2020-11-22 05:41

If I try to write

3.toFixed(5)

there is a syntax error. Using double dots, putting in a space, putting the three in parentheses or using br

4条回答
  •  灰色年华
    2020-11-22 06:13

    The period is part of the number, so the code will be interpreted the same as:

    (3.)toFixed(5)
    

    This will naturally give a syntax error, as you can't immediately follow the number with an identifier.

    Any method that keeps the period from being interpreted as part of the number would work. I think that the clearest way is to put parentheses around the number:

    (3).toFixed(5)
    

提交回复
热议问题