Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor.prototype not in the prototype chain?

related: Confusion about protype chain , primitives and objects

in Firebug console :

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

I think this should print true

like image 269
Ashkan Kh. Nazary Avatar asked Mar 29 '13 13:03

Ashkan Kh. Nazary


People also ask

Is constructor a prototype?

Every constructor has a prototype property, which will become the instance's [[Prototype]] when called via the new operator.

What is prototype and prototype chain?

Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.

What is a prototype How does it differ from a constructor?

So what's the difference between constructor and prototype? A short answer is that the constructor is a function that is used to create an object, while the prototype is an object that contains properties and methods that are inherited by objects created from a constructor.

What is the difference between __ proto __ and prototype?

All the objects have proto property. The prototype gives access to the prototype of function using function. proto gives access to the prototype of the function using the object.


1 Answers

a = 12 creates a primitive number, which is not quite the same as a Number object. Primitives are implicitly cast to objects for purposes of property access.

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

As per the ECMAScript spec:

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

  1. If V is not an object, return false.

primitive numbers are not, strictly speaking, objects.

like image 98
zzzzBov Avatar answered Sep 30 '22 10:09

zzzzBov