为什么布尔原语不调用原型 toString()?
Why does Boolean primitive not call prototype toString()?
假设我有这个代码:
Boolean.prototype.toString = function toString() {
return this.valueOf() ? '1' : '0';
};
var object = {
true: 'true',
false: 'false',
1: '1',
0: '0'
};
// "true" - this doesn't work
console.log('primitive', object[true]);
// "1" - but these do
console.log('primitive.toString()', object[true.toString()]);
console.log('instance', object[new Boolean(true)]);
为什么原语不使用 class 的 toString
定义?对象键是字符串或符号,它们不能只是原始布尔值。这就是我困惑的原因。
因为规范是这么说的。
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-tostring
在此 table 中定义了原语的字符串值。仅对对象使用 ToPrimitive。
table 告诉我们对象 o
的 ToString
是 ToString( ToPrimitive(o, "string"))
规范告诉我们,如果使用对象调用 ToPrimitive
,我们必须遵循以下步骤:
1. If PreferredType was not passed, let hint be "default".
2. Else if PreferredType is hint String, let hint be "string".
3. Else PreferredType is hint Number, let hint be "number".
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
5. ReturnIfAbrupt(exoticToPrim).
6. If exoticToPrim is not undefined, then
a. Let result be Call(exoticToPrim, input, «hint»).
b. ReturnIfAbrupt(result).
c. If Type(result) is not Object, return result.
d. Throw a TypeError exception.
7. If hint is "default", let hint be "number".
8. Return OrdinaryToPrimitive(input,hint).
@@toPrimitive
beeing set是一个特例所以我们现在要看OrdinaryToPrimitive
1. Assert: Type(O) is Object
2. Assert: Type(hint) is String and its value is either "string" or "number".
3. If hint is "string", then
a. Let methodNames be «"toString", "valueOf"».
4. Else,
a. Let methodNames be «"valueOf", "toString"».
5. For each name in methodNames in List order, do
a. Let method be Get(O, name).
b. ReturnIfAbrupt(method).
c. If IsCallable(method) is true, then
i. Let result be Call(method, O).
ii. ReturnIfAbrupt(result).
iii. If Type(result) is not Object, return result.
6. Throw a TypeError exception.
所以这意味着 ToPrimitive(o, "string")
的 return 值是 o.toString()
而 toString(o.toString())
与 o.toString()
相同。
- "true" 是布尔值。
- 一个"Boolean"实例是一个对象,不是布尔值。
- 因此实例没有义务使用原型。
- "Boolean"对象只是js中对boolean类型的抽象
Boolean.prototype.toString=function toString(){
return this?'1':'0';
};
var object = {
'true':'true',
'false':'false',
'1':'1',
'0':'0'
};
console.log('primitive', object[true]);
console.log('instance', object[new Boolean(true)]);
console.log('bool type:', typeof(true));
console.log('Boolean type:', typeof(new Boolean(true)));
假设我有这个代码:
Boolean.prototype.toString = function toString() {
return this.valueOf() ? '1' : '0';
};
var object = {
true: 'true',
false: 'false',
1: '1',
0: '0'
};
// "true" - this doesn't work
console.log('primitive', object[true]);
// "1" - but these do
console.log('primitive.toString()', object[true.toString()]);
console.log('instance', object[new Boolean(true)]);
为什么原语不使用 class 的 toString
定义?对象键是字符串或符号,它们不能只是原始布尔值。这就是我困惑的原因。
因为规范是这么说的。 http://www.ecma-international.org/ecma-262/6.0/index.html#sec-tostring 在此 table 中定义了原语的字符串值。仅对对象使用 ToPrimitive。
table 告诉我们对象 o
的 ToString
是 ToString( ToPrimitive(o, "string"))
规范告诉我们,如果使用对象调用 ToPrimitive
,我们必须遵循以下步骤:
1. If PreferredType was not passed, let hint be "default".
2. Else if PreferredType is hint String, let hint be "string".
3. Else PreferredType is hint Number, let hint be "number".
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
5. ReturnIfAbrupt(exoticToPrim).
6. If exoticToPrim is not undefined, then
a. Let result be Call(exoticToPrim, input, «hint»).
b. ReturnIfAbrupt(result).
c. If Type(result) is not Object, return result.
d. Throw a TypeError exception.
7. If hint is "default", let hint be "number".
8. Return OrdinaryToPrimitive(input,hint).
@@toPrimitive
beeing set是一个特例所以我们现在要看OrdinaryToPrimitive
1. Assert: Type(O) is Object
2. Assert: Type(hint) is String and its value is either "string" or "number".
3. If hint is "string", then
a. Let methodNames be «"toString", "valueOf"».
4. Else,
a. Let methodNames be «"valueOf", "toString"».
5. For each name in methodNames in List order, do
a. Let method be Get(O, name).
b. ReturnIfAbrupt(method).
c. If IsCallable(method) is true, then
i. Let result be Call(method, O).
ii. ReturnIfAbrupt(result).
iii. If Type(result) is not Object, return result.
6. Throw a TypeError exception.
所以这意味着 ToPrimitive(o, "string")
的 return 值是 o.toString()
而 toString(o.toString())
与 o.toString()
相同。
- "true" 是布尔值。
- 一个"Boolean"实例是一个对象,不是布尔值。
- 因此实例没有义务使用原型。
- "Boolean"对象只是js中对boolean类型的抽象
Boolean.prototype.toString=function toString(){
return this?'1':'0';
};
var object = {
'true':'true',
'false':'false',
'1':'1',
'0':'0'
};
console.log('primitive', object[true]);
console.log('instance', object[new Boolean(true)]);
console.log('bool type:', typeof(true));
console.log('Boolean type:', typeof(new Boolean(true)));