Skip to content
Advertisement

What is the end of prototype chain in JavaScript — null or Object.prototype?

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 and Object.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 prototype x.prototype? And how long would the prototype chain be? Would x.prototype have only one prototype, Object.prototype, making a 3 point chain?
  • How does JavaScript internally create automatic prototypes?

Advertisement

Answer

  1. 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 not prototype.

  2. Object.prototype, Animal.prototype are different from x.__proto__. The former are function objects (Object, Animal) having a prototype property pointing to a prototype object. And x.__proto__ is how the prototype chain is followed upward. To go up and up, it is x.__proto__.__proto__ and so on. See JavaScript’s Pseudo Classical Inheritance diagram to understand it more.

  3. 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.” So Object.prototype and null are not the same thing.

  4. All JavaScript objects will have obj.__proto__ referring ultimately to what Object.prototype refers to. If it is not obj.__proto__, then it is obj.__proto__.__proto__. If not, just go up, and up, and it will reach the prototype object which Object.prototype refers to. And at this point, when you go up one level (by adding a .__proto__, then you get null. 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
    
Advertisement