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

前端 未结 4 1923
醉话见心
醉话见心 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:03

    It doesn't work because JavaScript interprets the 3. as being either the start of a floating-point constant (such as 3.5) or else an entire floating-point constant (with 3. == 3.0), so you can't follow it by an identifier (in your case, a property-name). It fails to recognize that you intended the 3 and the . to be two separate tokens.

    Any of your workarounds looks fine to me.

提交回复
热议问题