this, Prototypes and Classes · Lesson 22 of 40
Prototypes and the Prototype Chain
Where does [1, 2].map come from? You never added a map property to your array. The answer is the prototype: every JavaScript object has a hidden link to another object, and when a property isn't found on the object itself, JavaScript follows that link. This is how inheritance works in JavaScript – classes are built on top of it.
In this lesson
- The prototype link and property lookup
- The prototype chain of arrays and objects
Object.createand constructor functions- Own vs inherited properties, shadowing
Property lookup follows the chain
const animal = {
eats: true,
describe() { return `${this.name} eats: ${this.eats}`; },
};
const dog = Object.create(animal); // dog's prototype is animal
dog.name = "Bruno";
dog.barks = true;
console.log(dog.barks); // own property
console.log(dog.eats); // found on the prototype
console.log(dog.describe()); // inherited method, this = dog
console.log(Object.getPrototypeOf(dog) === animal);
console.log(Object.keys(dog)); // own keys only
true true Bruno eats: true true [ 'name', 'barks' ]
Looking up dog.describe
- dogname, barks – no describe here
- animal (dog's prototype)eats, describe ✔ found
- Object.prototypetoString, hasOwnProperty…
- nullend of the chain → undefined
Built-in chains
const arr = [1, 2, 3];
console.log(Object.getPrototypeOf(arr) === Array.prototype);
console.log(Object.getPrototypeOf(Array.prototype) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
console.log(Object.hasOwn(arr, "map"), "map" in arr);
console.log(typeof Array.prototype.map);
true true null false true function
So an array's chain is arr → Array.prototype → Object.prototype → null. All arrays share one copy of map, filter and friends on Array.prototype – memory-efficient, and the reason methods are "available everywhere".
Shadowing
Writing a property always creates it on the object itself, hiding (shadowing) the inherited one without changing the prototype:
const base = { greeting: "Hello" };
const a = Object.create(base);
const b = Object.create(base);
a.greeting = "Namaste"; // own property on a
console.log(a.greeting, b.greeting, base.greeting);
delete a.greeting; // the inherited value shows again
console.log(a.greeting);
Namaste Hello Hello Hello
Constructor functions (the pre-class way)
Before ES2015, "classes" were written as functions called with new, with shared methods placed on the function's prototype property. You'll still see this in older code and interview questions:
function Account(owner, balance) {
this.owner = owner; // own data on each instance
this.balance = balance;
}
Account.prototype.deposit = function (amount) { // shared by all instances
this.balance += amount;
return this.balance;
};
const acc1 = new Account("Asha", 1000);
const acc2 = new Account("Ravi", 500);
console.log(acc1.deposit(250), acc2.deposit(100));
console.log(acc1.deposit === acc2.deposit);
console.log(Object.getPrototypeOf(acc1) === Account.prototype, acc1 instanceof Account);
1250 600 true true true
What new Account("Asha", 1000) does
- Create an empty object{}
- Link its prototypeto Account.prototype
- Run Account with this = the objectsets owner and balance
- Return the objectunless the function returns another object
prototype vs __proto__
| Term | What it is |
|---|---|
Fn.prototype | a property of a function: the object that instances created with new Fn() will link to |
[[Prototype]] | the hidden link every object has to its prototype |
obj.__proto__ | an old accessor for that link – use Object.getPrototypeOf / Object.setPrototypeOf instead |
Don't extend built-in prototypes
Adding methods to Array.prototype or String.prototype affects every array and string in the app, can clash with future JavaScript features and with libraries. Write a helper function instead.
Practice
Try it yourself
Create
vehiclewith astart()method and acarthat inherits it viaObject.create, then list only car's own keys.Show solution
const vehicle = { start() { return `${this.model} started`; } }; const car = Object.create(vehicle); car.model = "Nexon"; console.log(car.start(), Object.keys(car), "start" in car);Nexon started [ 'model' ] true
Quick check
What is at the end of every prototype chain?
null (after Object.prototype for ordinary objects).
Where does [].map live?
On Array.prototype, shared by all arrays.
Does assigning child.x = 1 change the prototype's x?
No – it creates an own property on child that shadows the inherited one.
Summary
- Every object links to a prototype; missing properties are looked up along the chain.
- Methods live once on the prototype and are shared by all instances.
- Constructor functions + prototype are what ES classes compile down to.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.