I’ve been reading about the prototype chain in JavaScript and came to two slightly different definitions. It is said that every object in JavaScript has a prototype and that prototype in turn has another prototype.
The top prototype (Grand) may also have prototype and the chain can continue. Now the chain will stop at one last object. JavaScript: The Good Parts says the chain terminates at Object.prototype
and MDN says null
is the final link where the chain terminates.
Javascript: The Good Parts
Every object is linked to a prototype object from which it can inherit properties. All objects created from object literals are linked to
Object.prototype
, an object that comes standard with JavaScript.
MDN
Each object has an internal 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. null, by definition, has no prototype, and acts as the final link in this prototype chain.
- What is the end of prototype chain in JavaScript — null or Object.prototype? Or are
null
andObject.prototype
one and the same thing? - Are objects, which are not created from object literals, not linked to
Object.prototype
? - Say I have an object
var x = { len: 4, breadth: 5}
. Would JavaScript automatically create its prototypex.prototype
? And how long would the prototype chain be? Wouldx.prototype
have only one prototype,Object.prototype
, making a 3 point chain? - How does JavaScript internally create automatic prototypes?
Advertisement
Answer
It is like, if New York has an envelope and inside, it says Colorado, and Colorado has an envelope, and inside, it says San Francisco, and San Francisco has an envelope, and inside, it says “none”. So is San Francisco end of the chain, or is “none” the end of chain? It may depend on how you look at it. But one thing is for sure: it points up and up the chain, for inheritance purpose (prototypal inheritance), until it reaches
null
, which means can’t go further up. And make sure you know that, to go up and up the chain, it is__proto__
. It is notprototype
.Object.prototype
,Animal.prototype
are different fromx.__proto__
. The former are function objects (Object, Animal) having aprototype
property pointing to a prototype object. Andx.__proto__
is how the prototype chain is followed upward. To go up and up, it isx.__proto__.__proto__
and so on. See JavaScript’s Pseudo Classical Inheritance diagram to understand it more.Object.prototype
refers to a prototype object. Quoted from MDN,null
“represents the intentional absence of any object value. It is one of JavaScript’s primitive values.” SoObject.prototype
andnull
are not the same thing.All JavaScript objects will have
obj.__proto__
referring ultimately to whatObject.prototype
refers to. If it is notobj.__proto__
, then it isobj.__proto__.__proto__
. If not, just go up, and up, and it will reach the prototype object whichObject.prototype
refers to. And at this point, when you go up one level (by adding a.__proto__
, then you getnull
. You can try it in Google Chrome’s developer’s tool:x = { a : 1 } > Object {a: 1} x.__proto__ === Object.prototype > true x.__proto__.__proto__ > null Object.prototype.__proto__ > null