Prototypes

Topic 1 of 4 in Classes & prototypes5 min readBeginnerFree course

Overview

How does [1, 2].map exist when you never added a map property to your array? Through prototypes.

Every object has a hidden link to another object, called its prototype. When you read a property that isn’t on the object itself, JavaScript looks on the prototype, then on its prototype, and so on. That path is the prototype chain.

JavaScript
const list = [1, 2];
Object.getPrototypeOf(list) === Array.prototype;  // true
list.hasOwnProperty('map');                       // false — it comes from Array.prototype

Prototypes are how JavaScript shares behaviour between objects. Instead of copying map, filter and fifty other methods into every array you create, all arrays link to one shared Array.prototype object that holds them.

Creating an object with a prototype

Object.create(proto) makes a new, empty object whose prototype is proto:

JavaScript
const animal = {
  speak() {
    return `${this.name} makes a sound`;
  },
};

const dog = Object.create(animal);
dog.name = 'Bruno';
dog.speak(); // 'Bruno makes a sound' — found on animal

dog doesn’t have speak itself; the lookup walks up to animal.

The chain dog → animal → Object.prototype → null. dog has name ‘Bruno’; animal has speak(); Object.prototype has toString() and hasOwnProperty(). dog.speak() isn’t found on dog, so it’s found on animal and called with this = dog. dog.fly() isn’t found anywhere, so it’s undefined.

Notice that this inside speak is still dog — this is the object the method was called on, not the object where it was found. That’s what makes shared methods work.

The end of the chain

Almost every chain ends with Object.prototype (which provides toString, hasOwnProperty and friends), and its prototype is null. If a property isn’t found by then, the result is undefined.

JavaScript
Object.getPrototypeOf(Object.prototype); // null
dog.fly;                                 // undefined — searched the whole chain

You can make an object with no prototype using Object.create(null). It has no inherited properties at all, which is occasionally useful for pure dictionaries.

Shadowing

If the object has its own property with the same name, that one wins:

JavaScript
dog.speak = function () { return 'Woof!'; };
dog.speak(); // 'Woof!'
animal.speak.call(dog); // 'Bruno makes a sound' — the original is still there

Reading walks up the chain, but writing always puts the property on the object itself. Assigning dog.speak never changes animal.

Own properties versus inherited ones

JavaScript
Object.hasOwn(dog, 'name');   // true  — dog's own
Object.hasOwn(dog, 'speak');  // true now (we shadowed it)
'toString' in dog;            // true  — in checks the whole chain
Object.keys(dog);             // ['name', 'speak'] — own, enumerable properties only

for…in loops include inherited enumerable properties, which is one reason Object.keys and Object.entries are usually preferred.

Inspecting prototypes

JavaScript
Object.getPrototypeOf(dog) === animal; // true
animal.isPrototypeOf(dog);             // true
[] instanceof Array;                   // true — Array.prototype is in the chain

In the console you’ll see the link displayed as [[Prototype]]. Old code sometimes uses dog.__proto__; it works, but Object.getPrototypeOf is the official way.

Constructor functions

Before class existed, prototypes were usually set up with constructor functions. Every function has a prototype property, and objects created with new link to it:

JavaScript
function Course(title) {
  this.title = title;
}
Course.prototype.describe = function () {
  return `Course: ${this.title}`;
};

const js = new Course('JavaScript');
js.describe(); // 'Course: JavaScript'
Object.getPrototypeOf(js) === Course.prototype; // true

This is exactly what a class does behind the scenes, as the next topic shows.

Why it matters

You’ll rarely create prototypes by hand — class does it for you. But knowing the chain explains things like why methods are shared between instances (saving memory), how instanceof works, and what you’re looking at when you expand [[Prototype]] in DevTools.

Don’t extend built-in prototypes such as Array.prototype in your own code. It can clash with future versions of JavaScript and with libraries.

Exercise

Share a method

Create a shape object with an area() method that returns this.width * this.height. Use Object.create to make rect with shape as its prototype, give rect its own width and height, and call rect.area().

JavaScript
const shape = {
  // add area() here
};

// create rect here

Summary

  • Every object has a hidden link to another object: its prototype.
  • Missing properties are looked up along the prototype chain, which ends at null.
  • Writing a property always creates it on the object itself, shadowing the prototype’s.
  • Methods on a prototype are shared by every object that links to it.
  • Classes are a friendlier syntax over the same mechanism.