JavaScript Prototypes Explained: Prototype Chain and Classes

Learn how JavaScript prototype lookup lets objects share methods, how ES6 classes use prototypes, and why Array methods live on Array.prototype.

JavaScript Prototypes Explained: Prototype Chain and Classes

What is a Prototype in JavaScript?

Create two objects that need the same method. You could copy the method into both, but then you'd have duplicate functions. JavaScript offers another approach: an object can look up a property on another object connected to it. That connected object is its prototype.

When a property isn't found directly on an object, JavaScript checks its prototype, then that prototype's prototype, continuing up the prototype chain until it finds the property or reaches the end. This is how objects can share behavior without storing a copy of every method on every instance.

You can run these examples in a modern browser console or recent Node.js.

An object can use a method from its prototype

Object.create() makes a new object with the object you provide as its prototype:

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

const dog = Object.create(animal);
dog.name = "Rex";

console.log(dog.speak()); // Rex makes a sound.

dog doesn't have its own speak property. When JavaScript evaluates dog.speak, it checks dog, then looks at animal and finds the method. The call dog.speak() sets this to dog, so the inherited method reads dog.name.

You can inspect the relationship:

console.log(Object.getPrototypeOf(dog) === animal); // true
console.log(Object.hasOwn(dog, "speak")); // false
console.log("speak" in dog); // true

Object.hasOwn() checks only the object itself. The in operator checks the object and its prototype chain.

An own property can shadow an inherited one

If an object gets a property with the same name as one on its prototype, JavaScript finds the own property first. This is called shadowing:

dog.speak = function () {
  return `${this.name} barks.`;
};

console.log(dog.speak()); // Rex barks.
console.log(Object.hasOwn(dog, "speak")); // true

The original animal.speak method is still there. The new dog.speak simply takes precedence when looking up that property on dog.

How ES6 classes use prototypes

JavaScript classes provide a familiar syntax for creating objects that share methods. Methods declared in a class body are placed on the class's prototype, rather than copied onto each instance:

class User {
  constructor(name) {
    this.name = name;
  }

  greet() {
    return `Hello, ${this.name}!`;
  }
}

const firstUser = new User("Mina");
const secondUser = new User("Nafi");

console.log(firstUser.greet()); // Hello, Mina!
console.log(Object.hasOwn(firstUser, "greet")); // false
console.log(Object.getPrototypeOf(firstUser) === User.prototype); // true

Both instances can use greet, but the method lives on User.prototype. Classes are built on JavaScript's prototype system; they don't replace it with a separate inheritance mechanism.

Why does [1, 2, 3].map() work?

Array instances inherit standard methods from Array.prototype. That's why you can call methods such as map, filter, and reduce on arrays without defining them on each array yourself:

const numbers = [1, 2, 3];

console.log(Object.hasOwn(numbers, "map")); // false
console.log(Object.getPrototypeOf(numbers) === Array.prototype); // true
console.log(numbers.map((number) => number * 2)); // [2, 4, 6]

The map method is shared through the prototype. It creates a new array containing the callback results.

Prototype lookup has an end

JavaScript follows the chain until it finds the property or reaches null. Some objects, such as ones made with Object.create(null), have no prototype:

const dictionary = Object.create(null);
dictionary.topic = "prototypes";

console.log(Object.getPrototypeOf(dictionary)); // null

This is less common for everyday beginner code, but it helps explain that not every object inherits from Object.prototype.

Common beginner mistakes

Thinking an inherited method is copied to every object

The object can use a method without owning it. Check with Object.hasOwn() to distinguish an own property from one found through the prototype chain.

Confusing prototype and an object's prototype

User.prototype is the object that instances made with new User() use as their prototype. You can inspect an instance's prototype with Object.getPrototypeOf(instance). The two are related, but they are not the same object as the instance.

Editing built-in prototypes for convenience

Avoid adding custom methods to Array.prototype or other built-in prototypes in regular application code. It can cause name conflicts and surprising behavior in libraries. Create your own helper function or class instead.

Practice

Create an object called vehicle with a method that returns a description using this. Use Object.create(vehicle) to create a car, then add its own properties and call the inherited method. Check which properties are own properties and which are inherited. Finally, compare that with a small class whose instances share a method.

What we learned

  • A prototype is an object another object consults when a property isn't found on itself.
  • JavaScript searches up the prototype chain until a property is found or the chain ends.
  • Objects can share methods through prototypes rather than copying each function onto every instance.
  • ES6 class methods are stored on the class's prototype.
  • Array methods such as map, filter, and reduce are available through Array.prototype.

Conclusion

Prototypes are the lookup system behind JavaScript's object sharing and inheritance. You may write most of your code with classes, but understanding prototypes explains where shared methods live and how JavaScript finds them. Try the vehicle example, then inspect a familiar array's prototype to see the same idea in action.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...