01 / 21

Prototypes &
Prototype Chain

Discover how JavaScript objects inherit properties and methods through an invisible chain: the foundation of object-oriented JavaScript.

JavaScript

What Is a JavaScript Object?

In JavaScript, an object is a collection of key-value pairs. Keys are strings (called properties), and values can be any data type: numbers, strings, functions, or even other objects.

const dog = { name: "Buddy", breed: "Golden Retriever", bark: function() { return "Woof!"; } };

What Is a Prototype?

A prototype is a regular JavaScript object from which other objects inherit properties and methods. Think of it as a blueprint parent: anything the parent has, its children can access automatically.

[[Prototype]]
Every object has a hidden internal slot called [[Prototype]] that points to its prototype object.
__proto__
The accessor property that lets you read or set an object's prototype (non-standard but widely supported).

Why Do Prototypes Matter?

Memory Efficiency
Shared methods live in one place (the prototype), not copied into every instance.
Inheritance
Objects can inherit behavior without duplicating code: a core OOP principle.
Dynamic Extension
Add a method to the prototype and every linked object gains access instantly.

The Prototype Chain

When you access a property, JavaScript follows a lookup chain: it checks the object itself, then its prototype, then that object's prototype, and so on until it reaches null.

myDog name: "Buddy" [[Prototype]] Dog.prototype bark(), fetch() [[Prototype]] Object.prototype toString(), etc. null Lookup: myDog.toString() 1. Check myDog → not found 2. Check Dog.prototype → not found 3. Check Object.prototype → ✓ found!

The Chain Ends at null

Object.prototype sits at the top of every prototype chain. Its own prototype is null: the chain always terminates. If a property isn't found anywhere in the chain, JavaScript returns undefined.

1
Property found on the object itself? Return its value.
2
Not found? Move up to the prototype and check again.
3
Reached null? The property doesn't exist: return undefined.

__proto__ vs .prototype

These two are the most confused concepts in JavaScript prototypes. They are related but serve very different purposes.

__proto__ .prototype
Lives on Every object Only on functions
Purpose Points to this object's prototype Becomes the prototype of objects created by this function
Example myDog.__proto__ Dog.prototype
Preferred API Object.getPrototypeOf() Direct property access

How new Wires the Chain

When you call a function with the new keyword, JavaScript performs four hidden steps to connect the prototype chain.

1
Create a brand-new empty object: {}.
2
Link the new object's __proto__ to the constructor's .prototype.
3
Run the constructor function with this set to the new object.
4
Return the new object (unless the constructor explicitly returns another object).

How to Trace the Chain

Follow these steps to trace any prototype chain and understand property lookups.

1
Identify the object and the property you're accessing.
2
Check own properties first using obj.hasOwnProperty("prop").
3
Follow __proto__ (or use Object.getPrototypeOf(obj)) to the next object in the chain.
4
Repeat until you find the property or reach null.
5
If null is reached, the result is undefined.

Example 1: Basic Chain

function Dog(name) { this.name = name; } Dog.prototype.bark = function() { return this.name + " says Woof!"; }; const buddy = new Dog("Buddy"); buddy.bark(); // "Buddy says Woof!"
buddy name: "Buddy" Dog.prototype bark() Object.prototype toString(), ... Own property Inherited method Built-in methods

Example 2: Property Shadowing

function Animal(sound) { this.sound = sound; } Animal.prototype.sound = "..."; Animal.prototype.speak = function() { return this.sound; }; const cat = new Animal("Meow"); cat.speak(); // "Meow" — own property wins!

Example 3: Object.create()

Object.create() lets you build a prototype chain without constructor functions.

const vehicle = { start: function() { return "Vroom!"; } }; const car = Object.create(vehicle); car.wheels = 4; car.start(); // "Vroom!" — from vehicle car.wheels; // 4 — own property
car wheels: 4 vehicle start() Object.prototype toString(), ...

Classes Are Syntactic Sugar

ES6 class syntax looks like classical inheritance, but under the hood it's still prototypes. The class keyword doesn't create a new mechanism: it wraps the same constructor + prototype pattern in cleaner syntax.

class Dog { constructor(name) { this.name = name; } bark() { return this.name + " says Woof!"; } } // Dog.prototype.bark is STILL where bark() lives!

Own vs Inherited Properties

JavaScript gives you tools to distinguish between properties an object owns directly and properties it inherits from the chain.

const buddy = new Dog("Buddy"); buddy.hasOwnProperty("name"); // true — own buddy.hasOwnProperty("bark"); // false — inherited "name" in buddy; // true — checks whole chain "bark" in buddy; // true — found on prototype
hasOwnProperty()
Returns true only if the property is directly on the object, not inherited.
in operator
Returns true if the property exists anywhere in the prototype chain.

Common Mistakes

✕ Confusing __proto__ with .prototype
Remember: __proto__ is on every object (the link). .prototype is only on functions (the blueprint).
✕ Overwriting .prototype entirely
Replacing Dog.prototype = {} breaks the constructor reference and can disconnect existing instances.
✕ Thinking classes use a different mechanism
ES6 classes use the exact same prototype chain. They don't introduce classical inheritance.

Summary at a Glance

Concept What It Does Key Detail
Prototype Object that others inherit from Every object has one (except null)
Prototype Chain Linked list of prototypes Ends at Object.prototype → null
__proto__ Link to the actual prototype Present on every object
.prototype Blueprint for instances Only on constructor functions
new keyword Creates + links objects Sets __proto__ = Fn.prototype
Object.create() Manual chain linking No constructor needed

Question 1 of 5

What happens when you access a property that doesn't exist on an object?

Question 2 of 5

What is the difference between __proto__ and .prototype?

Question 3 of 5

Given the code below, what does buddy.hasOwnProperty("bark") return?

Question 4 of 5

What sits at the very top of every prototype chain?

Question 5 of 5

ES6 class syntax in JavaScript introduces: