javascript - 为什么 2..toString() 有效?

标签 javascript

<分区>

为什么 2..toString() 返回 22.toString() 抛出此错误?

例子:

console.log(2..toString()); // prints 2

// Firefox throws the error
// `SyntaxError: identifier starts immediately after numeric literal`
console.log(2.toString());

var x = 2;
console.log(x.toString()); // prints 2

// Firefox throws the error
//`TypeError: XML descendants internal method called on incompatible Number`
console.log(x..toString());

最佳答案

那是因为2.被解析为2.0,所以2..toString()等价于2.0.toString( ),这是一个有效的表达式。

另一方面,2.toString() 被解析为 2.0toString(),这是一个语法错误。

关于javascript - 为什么 2..toString() 有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15458774/

相关文章:

javascript - Zurb Foundation 6 - 轨道不起作用

javascript - AngularJS 中的链接选项

javascript - 为什么我无法从 async 修饰的函数中获得预期结果?

javascript - 无法从公共(public) API 获取响应(在 React.js 中)

javascript - 如何通配符匹配 querySelectorAll 中的类名?

javascript - 使用 Express.JS 的基本 MVC 原则

javascript - 使用 bootstrap3 制作侧边栏

javascript - HTML 和 JavaScript - 如何正确保存纯文本

javascript - 变量在 JavaScript 本地函数中时没有获取值

javascript - 是否可以将 Observables 分组到对象而不是数组中?