为什么我无法使用单个点访问整数的属性?
如果我尝试编写,
3.toFixed(5)
则会出现语法错误。使用双点、插入空格、将三个点放在括号中或使用方括号表示法可以使其正常工作。
3..toFixed(5)
3 .toFixed(5)
(3).toFixed(5)
3["toFixed"](5)
为什么单点表示法不起作用?我应该使用其中哪一种替代方法?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
句点是数字的一部分,因此代码将被解释为:
这自然会产生语法错误,因为您不能立即在数字后面加上标识符。
任何防止句点被解释为数字一部分的方法都可以。我认为最清晰的方法是在数字两边加上括号:
The period is part of the number, so the code will be interpreted the same as:
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:
由于 JavaScript 标记生成器中的缺陷,您无法访问它。 JavaScript 尝试将数字上的点表示法解析为浮点文字,因此您不能在其后面使用属性或方法:
2.toString(); // raises SyntaxError
正如您所提到的,有一些解决方法可用于使数字文字也充当对象。其中任何一个都同样有效。
要了解对象用法和属性背后的更多信息,请查看 Javascript Garden。
You can't access it because of a flaw in JavaScript's tokenizer. Javascript tries to parse the dot notation on a number as a floating point literal, so you can't follow it with a property or method:
2.toString(); // raises SyntaxError
As you mentioned, there are a couple of workarounds which can be used in order make number literals act as objects too. Any of these is equally valid.
To understand more behind object usage and properties, check out the Javascript Garden.
它不起作用,因为 JavaScript 将
3.
解释为浮点常量(例如3.5
)的开头,或者整个 em> 浮点常量(使用3.== 3.0
),因此您不能在其后面添加标识符(在您的情况下为属性名称)。它无法识别您希望将3
和.
作为两个单独的标记。你的任何解决方法对我来说都很好。
It doesn't work because JavaScript interprets the
3.
as being either the start of a floating-point constant (such as3.5
) or else an entire floating-point constant (with3. == 3.0
), so you can't follow it by an identifier (in your case, a property-name). It fails to recognize that you intended the3
and the.
to be two separate tokens.Any of your workarounds looks fine to me.
这是 Javascript 语法中的歧义。当解析器获得一些数字然后遇到点时,它可以在“NumberLiteral”(如 3.5)或“MemberExpression”(如 3.foo)之间进行选择。我猜想由于科学计数法的原因,这种歧义无法通过前瞻解决 - 应该将
3.e2
解释为300
还是a property e2 of 3
?因此,他们自愿决定在这里更喜欢NumberLiteral
,只是因为实际上对3.foo
这样的东西的需求并不多。This is an ambiguity in the Javascript grammar. When the parser has got some digits and then encounters a dot, it has a choice between "NumberLiteral" (like 3.5) or "MemberExpression" (like 3.foo). I guess this ambiguity cannot be resolved by lookahead because of scientific notation - should
3.e2
be interpreted as300
ora property e2 of 3
? Therefore they voluntary decided to preferNumberLiteral
s here, just because there's actually not very much demand for things like3.foo
.正如其他人提到的,Javascript 解析器将整数文字后面的点解释为小数点,因此它不会调用 Number 对象上的方法或属性。
要显式通知 JS 解析器调用整数文字上的属性或方法,您可以使用以下任一选项:
两点表示法
用空格分隔
将整数写为小数 括
在括号中 分配
给常量或变量
As others have mentioned, Javascript parser interprets the dot after Integer literals as a decimal point and hence it won't invoke the methods or properties on Number object.
To explicitly inform JS parser to invoke the properties or methods on Integer literals, you can use any of the below options:
Two Dot Notation
Separating with a space
Write integer as a decimal
Enclose in parentheses
Assign to a constant or variable