Inheritance using prototype chain
JavaScript objects can inherit properties and methods from other objects through a link called the prototype. When you read a property that an object does not have, JavaScript looks at its prototype, then the prototype's prototype, and so on until it finds the property or reaches null. This sequence of links is the prototype chain, and it is how all inheritance in JavaScript works, including the class and extends syntax.
How a property lookup walks the chain
Every ordinary object has an internal link to another object, readable with Object.getPrototypeOf(obj). An array, for example, links to Array.prototype, which holds map, filter and friends, and that links to Object.prototype, which holds toString and hasOwnProperty. Its prototype is null, the end of the chain.
Writing a property never walks the chain. Assigning obj.x = 1 creates x on obj itself, which then "shadows" any x further up.
Building a chain with constructor functions
Before classes, inheritance was set up by hand. It is worth seeing once, because classes do the same thing for you.
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
function Dog(name, breed) {
Animal.call(this, name); // run the parent constructor
this.breed = breed;
}
// Link Dog.prototype to Animal.prototype
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog; // repair the constructor reference
Dog.prototype.speak = function () {
return `${this.name} barks`; // overrides Animal's version
};
const rex = new Dog('Rex', 'Beagle');
rex.speak(); // "Rex barks"
Object.getPrototypeOf(rex) === Dog.prototype; // true
Object.getPrototypeOf(Dog.prototype) === Animal.prototype; // true
rex instanceof Animal; // true
The same chain with class syntax
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
speak() {
return `${super.speak()}, then barks`;
}
}
new Dog('Rex', 'Beagle').speak(); // "Rex makes a sound, then barks"
The class version creates the same links. super(name) replaces Animal.call(this, name), and super.speak() calls the parent method further up the chain.
Own properties versus inherited ones
Object.hasOwn(rex, 'name') is true because the constructor put name directly on the object. Object.hasOwn(rex, 'speak') is false; speak lives on the prototype. A for...in loop visits inherited enumerable properties too, while Object.keys() lists only own ones.
Pitfalls with prototype inheritance
Putting arrays or objects on a prototype is a subtle bug. If Animal.prototype.tags = [], every animal shares the same array, so pushing a tag onto one dog adds it to all of them. Keep per-instance data in the constructor and only methods on the prototype.
When setting up chains manually, people often write Dog.prototype = Animal.prototype. That makes both names point to the same object, so adding bark to dogs also gives every animal a bark method. Use Object.create as shown.
Finally, avoid adding methods to built-in prototypes such as Array.prototype. It can clash with future language features and other libraries.
Deep chains are another design smell. Every level makes it harder to see where a method comes from, and changing a parent can break distant children. Keep hierarchies shallow, often one or two levels. When objects just need to share some behaviour, composing them from smaller helper objects, or using plain functions, is often simpler than inheriting. You can inspect a chain in the browser console by expanding an object and opening its [[Prototype]] entry level by level.
The idea in one list
- Reads walk up the chain; writes stay on the object.
- Methods belong on the prototype; data belongs on the instance.
classandextendsbuild the same chain with clearer syntax.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.