Prototypes — JavaScript

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…

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

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

⚠️ Easy ways to break the prototype chain

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.

What you will learn