Intermediate · Scope, this, prototypes and classes · topic 23 of 61 · 25 min
Prototypes and the prototype chain
Explanation
Where do methods like push come from?
You create an array with [1, 2] and can immediately call .push or .map, but you never put those methods on it. They live on a shared object, Array.prototype. Your array has a hidden link to it. This link is called the object's prototype, written [[Prototype]] in the specification.
Property lookup walks the chain
When you read obj.x, the engine checks obj itself. If x is not there, it checks obj's prototype, then the prototype's prototype, until it finds x or reaches null at the end of the chain (then the result is undefined). Writing obj.x = 1 always creates or changes the property on obj itself.
const nums = [1, 2];
console.log(Object.getPrototypeOf(nums) === Array.prototype); // true
console.log(Object.getPrototypeOf(Array.prototype) === Object.prototype); // true
console.log(Object.getPrototypeOf(Object.prototype)); // null (end)
// nums → Array.prototype → Object.prototype → nullObject.create: choose the prototype
const animal = {
speak() { return `${this.name} makes a sound`; },
};
const dog = Object.create(animal); // dog's prototype is animal
dog.name = 'Tommy';
console.log(dog.speak()); // Tommy makes a sound (found on animal)
console.log(Object.hasOwn(dog, 'speak')); // false: it is inheritedConstructor functions: the older way
Before classes (ES2015), people wrote constructor functions and called them with new. Every function has a .prototype object; objects made with new F() get F.prototype as their prototype. Methods placed on F.prototype are shared by all instances, so they exist in memory only once.
function Account(owner, balance) {
this.owner = owner;
this.balance = balance;
}
Account.prototype.deposit = function (amount) {
this.balance += amount;
return this.balance;
};
const a1 = new Account('Asha', 100);
const a2 = new Account('Ravi', 50);
console.log(a1.deposit(25)); // 125
console.log(a1.deposit === a2.deposit); // true: one shared functionWhat new does, step by step: it creates an empty object, sets its prototype to F.prototype, runs F with this = that object, and returns it.
Shadowing
If an object has its own property with the same name as one on the prototype, its own property wins. This is called shadowing, and it is how a class overrides an inherited method.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Prototypes and the prototype chain · 1/6
In this lesson you will see how objects inherit from other objects through prototypes.
Prefer a video by a teacher? Find free videos on “javascript prototype chain explained object create” on YouTube ↗
In real life: Thousands of order objects, one copy of each method
An order-management system loads 50,000 orders into memory. If every order object carried its own copy of total(), format() and validate(), memory use would grow badly. Putting the methods on a prototype (or in a class) means 50,000 small data objects all sharing one copy of each method.
const orderMethods = { total() { return this.qty * this.price; } };
const orders = [1, 2, 3].map((n) => Object.assign(Object.create(orderMethods), { qty: n, price: 100 }));
console.log(orders.map((o) => o.total())); // [100, 200, 300]
console.log(orders[0].total === orders[2].total); // truePractice (0/3 done)
Create an object cat with Object.create(animal) so that cat.speak() works, and give cat its own name "Billi".
Write a constructor function Product(name, price). Put a method withGst() on Product.prototype that returns price * 1.18 rounded to the nearest rupee. All products must share the same withGst function.
Write chainLength(obj) that returns how many prototypes are above obj before reaching null. For a plain object {} the answer is 1 (Object.prototype). For an array it is 2.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- ClassesScope, this, prototypes and classes · same module
- Hoisting and the temporal dead zoneScope, this, prototypes and classes · same module
- FunctionsC Programming (from zero to systems) · Functions and arrays
- Functions: declare, define, call and returnC++ Programming (modern C++ and DSA) · Decisions, loops and functions
- Classes, objects and constructorsC# and .NET · Classes and objects
- Constructors and thisJava Programming (Core to Spring Boot) · Classes and objects
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Where does [1, 2].map come from?
Answer: Array.prototype
map is defined once on Array.prototype and found through the chain.
What is at the very end of every normal prototype chain?
Answer: null
Object.prototype's prototype is null, which ends the lookup.
Why put methods on F.prototype rather than inside the constructor?
Answer: All instances share one copy, saving memory
Methods created inside the constructor are copied for each object.
const p = { x: 1 }; const c = Object.create(p); c.x = 2; What is p.x?
Answer: 1
Writing c.x creates an own property on c that shadows p.x; p is unchanged.
Where do the methods of a class live?Related
What is a prototype for?Related
Answer: To declare the function's signature before it is used
The compiler can then check calls.
From Functions
What does Object.keys({ a: 1, b: 2 }) return?Related
What is a function prototype?Related
Answer: A declaration: the function's first line ending with a semicolon
It tells the compiler the function's name, return type and parameter types before its body appears.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Scope, this, prototypes and classes” before its exam: Classes, Inheritance and composition.
- Module 5 exam: Scope, this, prototypes and classes · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Modules, errors, JSON and dates. 61 topics left in the course before the final exam.