Javascript Boolean.prototype.toString () unexpected results

Working through the Javascript JavaScript Reference using my browser console. View boolean objects. I came across unexpected behavior with the following method:

Boolean.prototype.toString () Returns the string "true" or "false" depending on the value of the object. Overrides the Object.prototype.toString () method

If I create boolean true or false, they both return the same "false" from this method:

var t = Boolean(true);
var f = Boolean(false);

Boolean.prototype.toString(t);
> "false"
Boolean.prototype.toString(f);
> "false"

I understand that I can more reliably query an object with:

Boolean.prototype.constructor(t);
> true
Boolean.prototype.constructor(f);
> false

So I ask: can anyone explain, preferably with examples, why Boolean.prototype.toString (true) returns false?

, ? ? Boolean Object wrappers, . Mac, ? .

+4
2

, Boolean.prototype . , true.toString === Boolean.prototype.toString, true.toString() Boolean.prototype.toString.call(true).

Boolean.prototype.toString , this === Boolean.prototype. , , , (, Boolean.prototype - ):

  • , Type (B) - Object, [[Class]] B - "Boolean", b - [[PrimitiveValue]] B.

[[PrimitiveValue]] valueOf:

Boolean.prototype.valueOf(); //false

Boolean.prototype spec (15.6.4) false.

, , toString , , Boolean.prototype, .

: . toString : t.toString(), f.toString()

N.B. Boolean(true) ; true .

+5

Boolean.prototype.toString() "true" "false" , , .

, :

> var t = Boolean(true);
undefined
> var f = Boolean(false);
undefined
> t.toString();
'true'
> f.toString();
'false'
> Boolean.prototype.toString.apply(t);
'true'
> Boolean.prototype.toString.apply(f);
'false'
+2

Source: https://habr.com/ru/post/1532742/


All Articles