Prototypal Inheritance Theory
Prototypal inheritance lets objects delegate property lookups through prototype links, enabling shared behavior without classical class-based copy inheritance.
What it is
Every ordinary JavaScript object can reference another object as its prototype. If a property is missing on the object itself, lookup continues on the prototype chain.
Methods placed on a shared prototype are reused across many instances, which is memory-efficient compared with redefining methods on each object.
class syntax is mostly a layer over this prototype mechanism, so understanding prototype lookup clarifies how methods and inheritance truly work.
Interview framing: define Prototypal Inheritance in one sentence, then explain one concrete runtime behavior and one common pitfall with a short code example.
How it works
- Step 1: property access starts on the receiver object.
- Step 2: if the property is not own, JavaScript checks the object's prototype.
- Step 3: lookup continues up the chain until a match is found or the chain ends at null.
- Step 4: writes create or update own properties unless explicit prototype mutation occurs.
Common mistakes
Mutating __proto__ at runtime
Frequent prototype mutation can hurt performance and make object behavior harder to reason about.
Fix: Define prototypes during object creation with class, Object.create, or constructor patterns.
Confusing own properties with inherited properties
in and for...in include inherited properties, which can lead to unexpected logic or serialization bugs.
Fix: Use Object.hasOwn or hasOwnProperty when own-property checks are required.
Overwriting prototype without restoring constructor
Replacing a constructor prototype object can lose the expected constructor reference and metadata.
Fix: If replacing prototype, explicitly reset constructor and verify method definitions.
Interview questions
Common interview prompts with concise model answers.
Is class inheritance different from prototype inheritance?
class uses prototype inheritance under the hood. It offers cleaner syntax, but runtime method lookup still follows prototype chains.
class User {
greet() { return "hi"; }
}
const u = new User();
console.log(Object.getPrototypeOf(u) === User.prototype); // trueWhere should instance methods live?
Methods shared by all instances should be on the prototype to avoid per-instance duplication.
Can primitives use prototype methods?
Yes. JavaScript temporarily boxes primitives, allowing access to methods on Number.prototype, String.prototype, and others.
What ends the prototype chain?
The chain ends at null, typically after reaching Object.prototype for ordinary objects.
Related concepts
Continue with these concepts to strengthen your mental model.