为什么数字文字不能访问Number方法? [英] Why don't number literals have access to Number methods?
问题描述
如果查看ECMAScript 3规范,您会看到原始值类型Null和Undefined没有附带Null和Undefined对象。
If you look at the ECMAScript 3 specification you will see that primitive value types Null and Undefined don't have accompanying Null and Undefined Objects.
>> Null
ReferenceError: Null is not defined
其他原始值类型Number,String和Boolean类型确实包含可以从全局范围引用的Number,String和Boolean对象。
The other primitive value types Number, String and Boolean types do have accompanying Number, String and Boolean objects which you can reference from global scope.
>>Number
function Number() { [native code] }
>>Boolean
function Boolean() { [native code] }
这些原语的目的值类型是为各自的原始值类型提供 toString
和 valueOf
等方法:
The purpose for these primitive value types is to provide methods such as toString
and valueOf
for their respective primitive value types:
>>var n = 1;
>>n.toString();
"1"
与
>>var n = 1;
>>Number.prototype.toString.call(n);
"1"
布尔值和字符串也是这样的:
Booleans and strings also work that way:
>>var b = true;
>>b.toString();
"true"
>>Boolean.prototype.toString.call(b);
"true"
您可以看到原始值对象正在使用它们的方法当您尝试混合类型时,伴随对象:
You can see that the primitive value objects are using the methods of their accompanying object when you try to mix types:
>>Boolean.prototype.toString.call(n);
TypeError: Boolean.prototype.toString is not generic
>>Number.prototype.toString.call(b)
TypeError: Number.prototype.toString is not generic
有趣的是,对于布尔值和字符串文字类型,您可以直接从文字中调用这些方法:
Interestingly enough for boolean and string literal types, you can call these methods directly from the literal:
>>true.toString();
"true"
>>Boolean.prototype.toString.call(true)
"true"
>>"moo".toString();
"moo"
>>String.prototype.toString.call("moo")
"moo"
原始值null和undefined,因为它们没有伴随Null和Undefined对象不能做这些事情:
Primitive values null and undefined, since they don't have accompanying Null and Undefined objects cannot do these things:
>>Null
ReferenceError: Null is not defined
>>null.toString()
TypeError: Cannot call method 'toString' of null
原始值类型编号的行为类似于两者的混合。如果你直接使用Number的原型对象的方法,你可以在文字上调用 toString
:
Primitive value type number behaves like a mix of the two. You can call toString
on a literal if you directly use the Number's prototype object's method:
>>Number.prototype.toString.call(1);
"1"
但你不能像文字一样从文字中访问该方法字符串和布尔值:
But you cannot access the method from the literal itself like you can with strings and booleans:
>>1.toString()
SyntaxError: Unexpected token ILLEGAL
为什么数字文字的行为与boolean和string的行为不同,即使有一个Number对象?
Why is it that number literals behave differently from boolean and string even though there's a Number object?
推荐答案
你可以以相同的方式访问它,这是一个不同的解析问题,要做到这一点,使用稍微不同的语法:
You can access it the same way, it's a different parsing issue here, to do it, use a slightly different syntax:
(1).toString()
数字可以有小数,所以当你去解析代码,使用括号有效时,以小数结尾的语法有点模棱两可。当你看到这也是有效的时候会更清楚:
Numbers can have decimals, so the syntax for ending in a decimal is a bit ambiguous when you go to parse the code, use parenthesis to be valid. It's a bit clearer when you see that this is also valid:
(1.).toString()
然而只有 1.toString()
它试图解析为带有小数的数字,它会失败。
However with just 1.toString()
it's trying to parse as a number with a decimal, and it fails.
这篇关于为什么数字文字不能访问Number方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!