Make an empty object, const cat = {}, then call cat.toString(). The code runs and gives '[object Object]'. But you never wrote toString on that object. Where does the method come from?
It comes from the prototype of cat. Every object in JavaScript is linked to one other object, called its prototype. When a property is not on the object itself, JavaScript looks for it on that object's prototype. If it is not there either, JavaScript looks on the prototype of that prototype, and continues upward to the end. This search path is called the prototype chain.
Searching up to the prototype
The example below makes the object cat with the function Animal, then reads four properties. The comment on each line says where that property is found:
function Animal(name) {
this.name = name
}
Animal.prototype.greet = function () {
return 'I am ' + this.name
}
const cat = new Animal('Kitty')
cat.name // 'Kitty', cat's own
cat.greet() // 'I am Kitty', found on Animal.prototype
cat.toString() // '[object Object]', found on Object.prototype
cat.fly // undefined, found nowhere
function Animal(name)is a constructor function: an ordinary function that you call withnewto make objects.new Animal('Kitty')makes a new object. InsideAnimal,thispoints at that new object. Sothis.name = namegivescatanameproperty of its own.Animal.prototypeis the object that becomes the prototype of every object made bynew Animal(...). Methods placed onAnimal.prototype, such asgreet, are shared by all of those objects.cat.greet():cathas nogreet, so JavaScript goes up toAnimal.prototypeand findsgreetthere. Insidegreet,thisstill points atcat, sothis.nameis the name that belongs tocat.
The chain for cat: cat → Animal.prototype → Object.prototype → null. Each arrow means "if it is not here, look on the next object". Object.prototype is at the end of almost every chain, and toString is kept there. If the search reaches null (no more objects) and the property is still not found, the result is undefined.
The demo below uses class, a shorter way to write the same pattern (the next lesson covers it). The chain in that demo is dog → Dog.prototype → Animal.prototype → Object.prototype → null.
Methods on the prototype, data in the constructor
This example makes two objects from Animal, then compares greet on both:
const a = new Animal('Kitty')
const b = new Animal('Mochi')
a.greet === b.greet // true, one and the same function
a.__proto__ === Animal.prototype // true
- Data that differs per object (
name) is written inside the constructor, throughthis. - A method is written once on the prototype, outside the constructor:
Animal.prototype.greet = function () { ... }. Every object uses that same function, so no object gets its own copy. That is whya.greet === b.greetgivestrue. - Use
function, not an arrow function, for a method on the prototype. Only withfunctiondoesthispoint at the object that called the method. - The link from an object to its prototype is kept in the hidden property
__proto__. The recommended way to read the prototype isObject.getPrototypeOf(a).
⚠️ Easy ways to break the prototype chain
- An arrow function as a method.
Animal.prototype.greet = () => 'I am ' + this.namecannot read the object'sname, becausethisinside an arrow function is not the object that called the method. - Forgetting
new.const k = Animal('Kitty')withoutnewmakes no new object, sokdoes not hold an object. - Changing
Object.prototype. Almost every object is affected, so bugs can appear anywhere. Never add anything toObject.prototype.
TL;DR: Every object is linked to another object called its prototype. A property that is not on an object is looked up along the prototype chain: on the prototype, then on the prototype of that prototype, until null (no more objects). A constructor function is a function that you call with new to make objects. It stores data through this, and its methods are written once on FunctionName.prototype, with function. The link from an object to its prototype is kept in __proto__. Object.prototype at the end of the chain provides methods such as toString.
⚡ A method you never wrote
Write:
const cat = {}
cat.toString()
Guess first: does it work or throw an error?
It works, and gives '[object Object]'. toString is not on cat, but JavaScript finds it on another object that cat is linked to: Object.prototype.
Meaning: Every object is linked to another object called its prototype. A property that is not on an object is looked up on its prototype, then on the prototype of that prototype, upward until null (no more objects). This search path is called the prototype chain. If the property is still not found at the end, the result is undefined.
⚡ A constructor and its prototype
Write:
function Animal(name) {
this.name = name
}
Animal.prototype.greet = function () {
return 'I am ' + this.name
}
const cat = new Animal('Kitty')
cat.greet() // 'I am Kitty'
Meaning: Animal is a constructor function: a function that you call with new to make objects. new Animal('Kitty') makes a new object, and this.name becomes that object's own property. The prototype of that new object is Animal.prototype, where the method greet is placed. greet is not on cat, so the search goes up and finds it there. The chain: cat → Animal.prototype → Object.prototype → null.
⚡ One method for all
Write:
const a = new Animal('Kitty')
const b = new Animal('Mochi')
a.greet === b.greet // true
Meaning: a and b use one and the same greet function, which is kept on Animal.prototype. Data that differs per object is written in the constructor, through this. A method is written once on the prototype, with function and not an arrow function, so that this points at the object that called the method.