为什么我不能用单个点访问整数的属性? [英] Why can't I access a property of an integer with a single dot?
问题描述
如果我尝试写
3.toFixed(5)
存在语法错误。使用双点,放入空格,将三个放在括号中或使用括号表示法使其正常工作。
there is a syntax error. Using double dots, putting in a space, putting the three in parentheses or using bracket notation allows it to work properly.
3..toFixed(5)
3 .toFixed(5)
(3).toFixed(5)
3["toFixed"](5)
为什么单点表示法不起作用?我应该使用哪一种替代方法呢?
Why doesn't the single dot notation work and which one of these alternatives should I use instead?
推荐答案
期间是数字的一部分,因此代码将被解释为:
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)
这篇关于为什么我不能用单个点访问整数的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!