Подтвердить что ты не робот

Почему булевский примитив не вызывает прототип toString()?

Скажем, у меня есть этот код:

Boolean.prototype.toString=function toString(){
  return this?'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)]);
4b9b3361

Ответ 1

Потому что спецификации так говорят. http://www.ecma-international.org/ecma-262/6.0/index.html#sec-tostring В этой таблице определены значения String для примитивов. Используется только для объектов ToPrimitive.


В таблице указано, что ToString для объекта o есть 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") равно o.toString(), а toString(o.toString()) совпадает с o.toString().

Ответ 2

  • "true" является логическим.
  • "Булевский" экземпляр - это объект, а не логический.
  • Таким образом, экземпляр не обязан использовать прототип.
  • Объект "Boolean" - это просто абстракция в js для использования булевого типа.

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)));

Ответ 3

Я пробую это на консоли, у меня не было ответа (почему?), но true не совпадает с new Boolean(true)

> true
< true
> new Boolean(true)
< Boolean {[[PrimitiveValue]]: true}
> true === new Boolean(true)
< false