为什么2..toString()有效? [英] Why does 2..toString() work?

查看:33
本文介绍了为什么2..toString()有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 2..toString()返回 2 ,但是 2.toString()会引发此错误?

Why does 2..toString() return 2 but 2.toString() throws this error?

示例:

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 is just a number, it doesn't have any methods to call.

2.可以强制转换为字符串,它是一个对象(即'2.0'),因此可以使用该方法.

2. can be coerced into a string, which is an object (i.e. '2.0'), hence can have the method.

仅将 2.toString()解析为 2.0tostring(),这当然没有意义.

Just 2.toString() will be parsed as 2.0tostring(), which of course doesn't make sense.

看看如何解析两者:

vs

用于生成这些代码的工具在这里: http://jsparse.meteor.com/

The tool to generate these is here by the way: http://jsparse.meteor.com/

这篇关于为什么2..toString()有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆