Objects, Prototypes & Delegation: How Inheritance Really Works
In languages like Java or C++, inheritance works by copying blueprints: when you instantiate a class, all methods are copied into the new object instance.
JavaScript does not work like that.
JavaScript is an object-based language built on Prototypal Delegation:
- Objects are linked directly to other objects in memory.
- When you ask an object for a method or property, if it doesn't possess it locally, it delegates the search up the Prototype Chain.
- ES6
classsyntax is simply syntactic sugar built on top of this exact prototype mechanism!
1. The Two Prototype Concepts: prototype vs. [[Prototype]]
The most common source of confusion for JavaScript developers is the difference between:
Function.prototype: The blueprint object attached to constructor functions and classes where shared methods live (e.g.,Array.prototype.map).[[Prototype]](or__proto__): The hidden internal pointer on every object instance that points to its parent prototype.
function Warrior(name) { this.name = name;}// 1. Defining a shared method on the prototype:Warrior.prototype.attack = function() { return `${this.name} strikes with sword!`;};// 2. Creating an instance:const hero = new Warrior("Aarav");// hero's internal [[Prototype]] points directly to Warrior.prototype:console.log(Object.getPrototypeOf(hero) === Warrior.prototype); // true2. Interactive Prototype Chain & Delegation Inspector
Click on different properties to watch how the JavaScript engine searches from the instance up through Warrior.prototype, Character.prototype, and Object.prototype until it hits null.
Prototype Chain & Inheritance Inspector
[[Prototype]] ExplorerWatch how JavaScript delegates property lookups up the prototype chain until it reaches Object.prototype or null.
Not found on 'myHero'. Looked up [[Prototype]] into Warrior.prototype and found attack(). Executed with 'this' bound to myHero!
When you write class Warrior extends Character {}, JavaScript does not create class blueprints like C++ or Java. Under the hood, it sets Object.setPrototypeOf(Warrior.prototype, Character.prototype).
myHero.hasOwnProperty("attack") returns false because attack is on the prototype, not the instance! Use the in operator ("attack" in myHero) to check the whole chain.
3. Why Prototypes Matter: Memory Efficiency
Consider what happens if you attach methods inside constructor functions versus on the prototype:
Bad Pattern: 1,000 Function Copies in Memory
function Player(name) { this.name = name; // ❌ Every time you call 'new Player()', a NEW function is allocated in heap memory! this.jump = function() { console.log("Jumping!"); };}const p1 = new Player("Aarav");const p2 = new Player("Priya");console.log(p1.jump === p2.jump); // false! (Two separate functions in memory)Good Pattern: 1 Shared Function via Prototype Delegation
function Player(name) { this.name = name;}// ✅ Only ONE single copy of jump() exists in memory for all instances:Player.prototype.jump = function() { console.log("Jumping!");};const p1 = new Player("Aarav");const p2 = new Player("Priya");console.log(p1.jump === p2.jump); // true! Both delegate to Player.prototype.jump4. hasOwnProperty() vs. The in Operator
Because properties can exist either directly on the instance or higher up the chain, JavaScript gives you two tools to inspect them:
| Tool | Inspects Own Properties? | Traverses Prototype Chain? |
|---|---|---|
obj.hasOwnProperty("key") | Yes | No (stops at the instance) |
"key" in obj | Yes | Yes (checks instance + all prototypes) |
const hero = new Warrior("Aarav");// 'name' was assigned directly in the constructor:console.log(hero.hasOwnProperty("name")); // true// 'attack' lives on Warrior.prototype:console.log(hero.hasOwnProperty("attack")); // false!console.log("attack" in hero); // true!5. ES6 class is Just Syntactic Sugar
In 2015, ECMAScript 6 introduced the class and extends keywords to make JavaScript feel more familiar to developers coming from Java or C#.
Under the hood, nothing changed:
// Modern ES6 Class:class Animal { eat() { return "Eating..."; }}class Dog extends Animal { bark() { return "Woof!"; }}const d = new Dog();// Under the hood, this sets up the exact same prototype link:console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype); // trueconsole.log(d.eat()); // "Eating..." (delegated to Animal.prototype)6. Flashcards & Core Concepts
What happens when an object property is accessed that doesn't exist on the instance?
What is the difference between 'in' and 'hasOwnProperty'?