What is prototypal inheritance and how does the prototype chain resolve properties?
Unlike classical class-based languages where classes inherit from classes, JavaScript uses prototypal inheritance: objects inherit directly from other objects.
Every JavaScript object has an internal `[[Prototype]]` link (accessible via `Object.getPrototypeOf(obj)`). When you access a property on an object, the JavaScript engine first checks the object's own properties. If not found, it walks up the prototype chain until it finds the property or reaches `null`.
ES6 `class` syntax is purely syntactic sugar over this prototypal mechanism.
const animal = {
eats: true,
walk() {
return "Animal is walking";
}
};
// Create a new object with 'animal' as its prototype
const rabbit = Object.create(animal);
rabbit.jumps = true;
console.log(rabbit.jumps); // true (own property)
console.log(rabbit.eats); // true (inherited from animal prototype)
console.log(rabbit.walk()); // "Animal is walking" (inherited method)
console.log(rabbit.fly); // undefined (reaches top of chain: Object.prototype -> null)Methods defined on prototypes are shared in memory among all instances, preventing redundant method allocations on every created object.
Ready for this Keyword & Binding?
Take a quick test to evaluate yourself, or move straight to the next module.