Constructor prototype not in prototype chain?

related: Confusion about the chain, primitives, and prototype objects

in the Firebug console:

a = 12 a.constructor.prototype.isPrototypeOf(a) // prints 'false' 

I think this should print true

+7
source share
2 answers

a = 12 creates a primitive number that does not quite match the Number object. Primitives are implicitly passed to objects to access properties.

 a = 12; //a is a primitive b = new Number(12); //b is an object a.constructor.prototype.isPrototypeOf(a); //false because a is primitive b.constructor.prototype.isPrototypeOf(b); //true because b is an object 

According to the ECMAScript specification :

When the isPrototypeOf method isPrototypeOf called with argument V, the following steps are performed:

  • If V is not an object, return false .

primitive numbers are not, strictly speaking, objects.

+9
source
 a = new Number(12); a.constructor.prototype.isPrototypeOf(a) // prints 'true' 

I'm not smart enough to tell you why I just know that the way it is. And yes, this is strange.

Now you can say: " 12 is a primitive, and new Number(12) is an object." But how do you explain this?

 (12).toFixed(3); // "12.000" 

Apparently, somewhere, JavaScript solves primitive power as well as being an object.

Why is there such a difference? How do you convert between two forms? How does this affect performance? All questions related to this question, to which I have no answer.

0
source

All Articles