What is Prototypal Inheritance in JavaScript?
How objects share properties and methods through the prototype chain.
There are times when you call a method on an object, like .toString() or
.map(), that you never wrote yourself. So where does it come from? The answer
is prototypal inheritance, the way JavaScript lets one object borrow properties
and methods from another.
Prototypal inheritance means every object has a hidden link to another object, its prototype. When a property isn't found on the object, JavaScript looks for it on the prototype.
The prototype chain
Every object has an internal link to its prototype. That prototype is just
another object, which has its own prototype, and so on until you reach null.
This is called the prototype chain.
When you read a property, JavaScript checks the object first. If it isn't there,
it checks the prototype, then the prototype's prototype, until it finds the
property or runs out of chain and returns undefined.
const numbers = [1, 2, 3]numbers.map((n) => n * 2) // [2, 4, 6]Object.getPrototypeOf(numbers) === Array.prototype // trueObject.getPrototypeOf(Array.prototype) === Object.prototype // trueObject.getPrototypeOf(Object.prototype) // null
The numbers array doesn't have its own map method. It finds it one step up
the chain on Array.prototype.
Creating an object with Object.create()
The simplest way to see this in action is Object.create(). It creates a new
object and sets its prototype to the object you pass in.
const animal = {eats: true,walk() {console.log(`${this.name} is walking`)},}const dog = Object.create(animal)dog.name = "Rex"dog.walk() // console: Rex is walkingdog.eats // trueObject.hasOwn(dog, "name") // trueObject.hasOwn(dog, "eats") // falseObject.getPrototypeOf(dog) === animal // true
dog only owns the name property. eats and walk are found on animal.
Notice this inside walk still points to dog, because this is the object
the method was called on, not the object where the method lives.
If you add a property with the same name to dog, it shadows the one on the
prototype. The prototype itself is left alone:
dog.eats = falsedog.eats // falseanimal.eats // true
Constructor functions and .prototype
Before classes, the common way to create many similar objects was a constructor
function. Every function has a prototype property, and any object created with
new gets that object as its prototype.
function Person(name) {this.name = name}Person.prototype.greet = function () {return `Hi, I'm ${this.name}`}const john = new Person("John")const jane = new Person("Jane")john.greet() // "Hi, I'm John"Object.getPrototypeOf(john) === Person.prototype // truejohn.greet === jane.greet // true
The greet method is stored once on Person.prototype and shared by every
person. That's the main benefit: methods aren't copied onto every object.
To inherit from another constructor, you link the prototypes together with
Object.create():
function Developer(name, language) {Person.call(this, name)this.language = language}Developer.prototype = Object.create(Person.prototype)Developer.prototype.constructor = DeveloperDeveloper.prototype.code = function () {return `${this.name} writes ${this.language}`}const sam = new Developer("Sam", "JavaScript")sam.greet() // "Hi, I'm Sam"sam.code() // "Sam writes JavaScript"
Now the chain looks like sam → Developer.prototype → Person.prototype →
Object.prototype → null.
Note: You may see __proto__ used to read or set a prototype. It's a legacy
feature kept for compatibility. Use Object.getPrototypeOf() and
Object.create() instead.
Classes are built on prototypes
The class syntax added in ES2015 doesn't change any of this. It's a cleaner
way to write the same constructor and prototype setup.
class Person {constructor(name) {this.name = name}greet() {return `Hi, I'm ${this.name}`}}class Developer extends Person {constructor(name, language) {super(name)this.language = language}}const sam = new Developer("Sam", "JavaScript")typeof Person // "function"Object.getPrototypeOf(sam) === Developer.prototype // trueObject.getPrototypeOf(Developer.prototype) === Person.prototype // true
Person is still a function, greet still lives on Person.prototype, and
extends links the prototypes just like we did by hand.
Summary
Objects in JavaScript inherit from other objects through the prototype chain.
Once that clicks, classes, built-in methods and this all make a lot more
sense.
Have some feedback on this post? .