LearnFrenzy
Sign in Take a free test

this, Prototypes and Classes · Lesson 22 of 40

Prototypes and the Prototype Chain

4 min read Updated Nov 7, 2026 Saurabh Samir

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.create and 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

Output

true
true
Bruno eats: true
true
[ 'name', 'barks' ]

Looking up dog.describe

  1. dogname, barks – no describe here
  2. animal (dog's prototype)eats, describe ✔ found
  3. Object.prototypetoString, hasOwnProperty…
  4. 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);

Output

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);

Output

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);

Output

1250 600
true
true true

What new Account("Asha", 1000) does

  1. Create an empty object{}
  2. Link its prototypeto Account.prototype
  3. Run Account with this = the objectsets owner and balance
  4. Return the objectunless the function returns another object

prototype vs __proto__

TermWhat it is
Fn.prototypea 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

  1. Create vehicle with a start() method and a car that inherits it via Object.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);

    Output

    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.

Next lesson

Search LearnFrenzy

Esc

Quick links