What is Prototypal Inheritance in JavaScript?
Learn how JavaScript prototypal inheritance works — the prototype chain, Object.create, constructor prototypes and how ES6 classes build on it, with examples.
Expected Interview Answer
Prototypal inheritance is JavaScript's mechanism where objects inherit properties and methods directly from other objects through an internal link called the prototype, rather than from classes.
Every object has a hidden reference to a prototype object. When you access a property that the object does not have, the engine walks up this prototype chain until it finds the property or reaches null. Functions used as constructors expose a prototype object shared by all their instances, and Object.create lets you build an object with any prototype you choose. The class syntax added in ES6 is largely syntactic sugar over this same prototype-based system.
- Shared methods live once on the prototype, saving memory
- Objects can inherit directly from other objects
- Behavior can be extended or overridden at runtime
- Object.create gives precise control over the prototype chain
- Underlies ES6 classes, which are sugar over prototypes
AI Mentor Explanation
Prototypal inheritance is like a cricket academy's house style. A young batter who lacks a specific shot falls back on the technique taught by their coach, and that coach in turn learned from the head coach. The player inherits skills up this lineage of mentors until someone in the chain supplies the stroke needed.
Step-by-Step Explanation
Step 1
Every object has a prototype
Objects carry an internal link to another object, their prototype, or to null.
Step 2
Property lookup starts locally
Accessing a property first checks the object's own properties.
Step 3
Walk the prototype chain
If not found, the engine follows the prototype link upward, level by level.
Step 4
Stop at a match or null
The search ends when the property is found or the chain reaches null (undefined).
Step 5
Share methods on the prototype
Put reusable methods on the prototype so all instances share one copy.
What Interviewer Expects
- That inheritance flows from objects via a prototype chain, not classes
- How property lookup walks the chain up to null
- The role of Object.create and constructor prototype
- That ES6 class is syntactic sugar over prototypes
- The difference between own properties and inherited ones
Common Mistakes
- Thinking JavaScript inheritance is class-based like Java under the hood
- Confusing the prototype property of a function with the object's __proto__
- Putting methods on each instance instead of on the prototype, wasting memory
- Forgetting hasOwnProperty when iterating and picking up inherited keys
- Mutating a shared prototype and unexpectedly affecting all instances
Best Answer (HR Friendly)
“Prototypal inheritance is how JavaScript objects borrow features from other objects. If an object does not have something you ask for, it looks it up on the object it was based on, and so on up a chain, which lets many objects share the same behavior efficiently.”
Code Example
const animal = {
describe() {
return `${this.name} makes a sound`;
},
};
// dog inherits from animal
const dog = Object.create(animal);
dog.name = 'Rex';
console.log(dog.describe()); // 'Rex makes a sound' — found on the prototype
console.log(dog.hasOwnProperty('describe')); // false — it's inherited
console.log(Object.getPrototypeOf(dog) === animal); // trueFollow-up Questions
- What is the difference between __proto__ and a function's prototype property?
- How does Object.create differ from using a constructor function?
- Are ES6 classes truly classes or sugar over prototypes?
- How do you check if a property is own versus inherited?
- What happens when you modify a prototype after instances are created?
MCQ Practice
1. When a property is not found on an object, JavaScript?
The engine follows the object's prototype link upward until it finds the property or reaches null.
2. Object.create(proto) returns?
Object.create builds a fresh object linked to the given prototype, so it inherits proto's properties.
3. ES6 class syntax in JavaScript is?
Classes are largely sugar over the existing prototype-based inheritance model.
Flash Cards
What is prototypal inheritance? — Objects inherit properties from other objects via a prototype chain, not from classes.
How is a missing property resolved? — The engine walks up the prototype chain until it finds it or hits null.
What does Object.create(proto) do? — Creates a new object whose prototype is proto, inheriting its properties.
Are ES6 classes real classes? — No — they are syntactic sugar over JavaScript's prototype-based inheritance.