Prototypal Inheritance in JavaScript — How It Actually Works
The Interview Question
How does prototypal inheritance work in JavaScript? How is it different from classical inheritance?
Expert Answer
In JavaScript, objects inherit directly from other objects through a prototype chain — there are no classes at the engine level (the class keyword is syntactic sugar). Every object has an internal [[Prototype]] link (accessible via Object.getPrototypeOf() or the __proto__ property). When you access a property on an object, JavaScript first checks the object itself. If the property isn't found, it walks up the prototype chain — checking the prototype, then the prototype's prototype, and so on until it reaches Object.prototype (which has a null prototype). This is fundamentally different from classical inheritance where classes define blueprints and instances are created from them. In prototypal inheritance, you create objects from other objects. When you use the class keyword in JavaScript, it's using prototypes behind the scenes — the constructor function's .prototype property becomes the [[Prototype]] of instances.
Key Points to Hit in Your Answer
- Objects inherit from objects, not from classes
- Property lookup walks the prototype chain until found or null
- class is syntactic sugar over constructor functions + prototypes
- Object.create() creates an object with a specified prototype
- hasOwnProperty checks only the object itself, not the chain
- Modifying a prototype affects all objects that inherit from it (even after creation)
Code Example
// Prototype chain in action
const animal = {
eat() { return 'eating'; }
};
const dog = Object.create(animal);
dog.bark = function() { return 'woof'; };
dog.bark(); // 'woof' — found on dog
dog.eat(); // 'eating' — found on animal (prototype)
dog.toString(); // found on Object.prototype
// Class syntax is sugar over prototypes
class Dog {
bark() { return 'woof'; }
}
// is equivalent to:
function Dog() {}
Dog.prototype.bark = function() { return 'woof'; };
// Proof:
const d = new Dog();
Object.getPrototypeOf(d) === Dog.prototype; // true
What Interviewers Are Really Looking For
The key insight is that JavaScript uses delegation, not copying. When an object inherits, it doesn't copy methods — it delegates via the prototype chain. Showing that class is sugar over prototypes demonstrates real understanding. Knowing hasOwnProperty vs. in operator for property checks is a common follow-up.
Practice This Question with AI Grading
Reading about interview questions is a start — but practicing with real-time AI feedback is how you actually get better. Goliath Prep grades your answers instantly and tells you exactly what you're missing.
Start Practicing Free →