Typorithm · medium

The Prototype Chain

prototype-chaininterview-questionsprototypeprotonew
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn The Prototype Chain

0%

Prototypes & Classes

Every object has an internal [[Prototype]] link, read via Object.getPrototypeOf or __proto__. Property reads walk up this chain until found or null. A constructor's .prototype becomes the [[Prototype]] of instances made with new — so .prototype lives on functions while __proto__ lives on instances.

Every JavaScript object has a hidden [[Prototype]] reference to another object (or null). You read it with Object.getPrototypeOf(obj) or the legacy obj.__proto__, and both return the same link.

When you read obj.prop, the engine looks on obj itself, then walks up the prototype chain — obj's [[Prototype]], then that object's [[Prototype]], and so on — until the property is found or the chain reaches null and you get undefined.

A constructor function carries a .prototype object, and new Ctor() creates an instance whose [[Prototype]] is set to Ctor.prototype. That is the key distinction: .prototype is a property of functions, while __proto__ is the per-instance link to it.

01

Object.getPrototypeOf / __proto__ read the [[Prototype]] link.

02

Reads walk the chain until found or null.

03

.prototype is on functions; __proto__ is on instances.

const arr = [1, 2, 3]
Object.getPrototypeOf(arr) === Array.prototype // true
arr.__proto__ === Array.prototype              // true — same thing
function Dog(name) { this.name = name }
Dog.prototype.bark = () => "woof"
const d = new Dog("Rex")
Object.getPrototypeOf(d) === Dog.prototype      // true

Read the implementation

Connect each part of the code to the concept before you type.

1. getPrototypeOf reads [[Prototype]]

Object.getPrototypeOf returns an object's internal [[Prototype]] link, here Array.prototype.

Object.getPrototypeOf(arr) === Array.prototype // true

2. __proto__ is the same link

__proto__ is the legacy accessor for the same [[Prototype]] that getPrototypeOf returns.

arr.__proto__ === Array.prototype              // true — same thing

3. .prototype lives on functions

A constructor function's .prototype object holds methods shared by every instance.

Dog.prototype.bark = () => "woof"

4. new wires the chain

new creates an object whose [[Prototype]] is set to Dog.prototype.

const d = new Dog("Rex")

5. Instance points at .prototype

The new instance's [[Prototype]] is exactly the constructor's .prototype object.

Object.getPrototypeOf(d) === Dog.prototype      // true

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

The Prototype Chain: Lesson & Practice · Typorithm | ApplyDjinn