Objects & OOP
Prototypes & inheritance
How objects inherit via the prototype chain.
Every JavaScript object has a hidden link to another object called its prototype. When you read a property, the engine walks up this prototype chain until it finds the property or reaches null.
The prototype chain is like asking your parents a question — if they do not know, they ask their parents, up the family tree until someone answers or you run out of ancestors.
Key concepts
1
class syntax is syntactic sugar over prototypes — extends sets up the chain and super calls the parent. Methods defined on a class live on the prototype, so all instances share one copy rather than duplicating functions.
syntactic sugarclassextendssuper
2
Understanding the chain explains why adding to Array.prototype affects every array, and why prototype lookups have a small performance cost for very deep chains.
Array.prototype
javascript
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
speak() { return `${this.name} barks`; } // overrides
}
const d = new Dog('Rex');
d.speak(); // 'Rex barks'
Object.getPrototypeOf(d) === Dog.prototype; // true