为什么我无法使用单个点访问整数的属性?

发布于 2025-01-07 20:45:53 字数 231 浏览 0 评论 0原文

如果我尝试编写,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三五鸿雁 2025-01-14 20:45:53

句点是数字的一部分,因此代码将被解释为:

(3.)toFixed(5)

这自然会产生语法错误,因为您不能立即在数字后面加上标识符。

任何防止句点被解释为数字一部分的方法都可以。我认为最清晰的方法是在数字两边加上括号:

(3).toFixed(5)

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)
摇划花蜜的午后 2025-01-14 20:45:53

由于 JavaScript 标记生成器中的缺陷,您无法访问它。 JavaScript 尝试将数字上的点表示法解析为浮点文字,因此您不能在其后面使用属性或方法:

2.toString(); // raises SyntaxError

正如您所提到的,有一些解决方法可用于使数字文字也充当对象。其中任何一个都同样有效。

2..toString(); // the second point is correctly recognized
2 .toString(); // note the space left to the dot
(2).toString(); // 2 is evaluated first

要了解对象用法和属性背后的更多信息,请查看 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.

2..toString(); // the second point is correctly recognized
2 .toString(); // note the space left to the dot
(2).toString(); // 2 is evaluated first

To understand more behind object usage and properties, check out the Javascript Garden.

从来不烧饼 2025-01-14 20:45:53

它不起作用,因为 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 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.

2025-01-14 20:45:53

这是 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 as 300 or a property e2 of 3? Therefore they voluntary decided to prefer NumberLiterals here, just because there's actually not very much demand for things like 3.foo.

情何以堪。 2025-01-14 20:45:53

正如其他人提到的,Javascript 解析器将整数文字后面的点解释为小数点,因此它不会调用 Number 对象上的方法或属性。

要显式通知 JS 解析器调用整数文字上的属性或方法,您可以使用以下任一选项:


两点表示法

3..toFixed()

用空格分隔

3 .toFixed()

将整数写为小数 括

3.0.toFixed()

在括号中 分配

(3).toFixed()

给常量或变量

const nbr = 3;

nbr.toFixed()

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

3..toFixed()

Separating with a space

3 .toFixed()

Write integer as a decimal

3.0.toFixed()

Enclose in parentheses

(3).toFixed()

Assign to a constant or variable

const nbr = 3;

nbr.toFixed()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文