← Back to JavaScript, page 2

javascript-0025 · Hard senior

How does prototypal inheritance work in JavaScript, and how do ES6 classes relate to it?

Every object has an internal link to a prototype object. When a property is not found on the object, the engine follows that link up the chain until it finds it or reaches null. Constructor functions and ES6 classes are different syntax over the same mechanism: methods live once on the prototype and are shared by all instances.

Full answer

In JavaScript, objects inherit directly from other objects. Each object has an internal slot called [[Prototype]], which can be read with Object.getPrototypeOf(obj). When code reads obj.speak, the engine checks obj itself first. If it is missing, it checks the prototype, then the prototype's prototype, and so on until it finds the property or reaches null and returns undefined. This is the prototype chain. Writing a property always creates it on the object itself (unless a setter is found), so assignment does not change the prototype.

There are three ways to set up a link:

  • Object.create(proto) makes a new object whose prototype is proto.
  • A constructor function called with new creates an object whose prototype is the function's prototype property. Methods are added to Constructor.prototype.
  • An ES6 class does the same with cleaner syntax. extends links the prototypes of the two classes and the constructor functions, and super calls up the chain. A class is still a function, and its methods are on Class.prototype.

Putting methods on the prototype means all instances share one copy, which saves memory compared with creating a function in every constructor call. instanceof walks the prototype chain, and Object.hasOwn tests only the object itself.

Classes add real differences: they are not hoisted for use, always run in strict mode, require new, and support private #fields. Modifying Object.prototype or built-in prototypes is considered unsafe.

javascript
const animal = {
  describe() {
    return `${this.name} makes ${this.sound()}`;
  },
  sound() {
    return 'a sound';
  },
};
const dog = Object.create(animal);
dog.name = 'Bruno';
dog.sound = () => 'a bark';
console.log(dog.describe());
console.log(Object.getPrototypeOf(dog) === animal, Object.hasOwn(dog, 'describe'));

function Person(name) {
  this.name = name;
}
Person.prototype.greet = function () {
  return `Hello, I am ${this.name}`;
};
const p1 = new Person('Anu');
const p2 = new Person('Vik');
console.log(p1.greet(), p1.greet === p2.greet);

class Employee extends Person {
  #salary;
  constructor(name, salary) {
    super(name);
    this.#salary = salary;
  }
  greet() {
    return `${super.greet()}, I work here`;
  }
  get band() {
    return this.#salary > 50000 ? 'senior' : 'junior';
  }
}
const e = new Employee('Ravi', 60000);
console.log(e.greet(), e.band);
console.log(e instanceof Employee, e instanceof Person, e instanceof Object);
console.log(typeof Employee, Object.getPrototypeOf(Employee) === Person);
console.log(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(e))) === Object.prototype);

p1.greet = () => 'shadowed';
console.log(p1.greet(), p2.greet());
try {
  Employee();
} catch (error) {
  console.log(error.name);
}

Run with Node.js v24.22.1 as js-25.js; two runs gave identical output. Output: Bruno makes a bark true false Hello, I am Anu true Hello, I am Ravi, I work here senior true true true function true true shadowed Hello, I am Vik TypeError

Key points

  • Property lookup follows the [[Prototype]] chain until null.
  • Assignment creates an own property; it does not change the prototype.
  • Object.create, constructor functions with new, and classes all build prototype links.
  • Methods on the prototype are shared by all instances.
  • Classes are syntax over prototypes, with extra rules such as strict mode and private fields.

Common mistakes

  • Saying classes replaced prototypes. They are built on them.
  • Defining methods inside a constructor and creating one copy per instance.
  • Assigning to an inherited property and expecting the prototype to change.
  • Replacing Constructor.prototype with a new object and forgetting to restore the constructor property.

Follow-ups

  • What is the difference between __proto__ and prototype?
  • How does super work in an object literal method or in a static method?
  • What happens to the chain when you use Object.create(null)?

Tips

  • Draw the chain: instance, then Class.prototype, then Object.prototype, then null. A sketch makes the answer concrete.
  • Show that methods are shared with the p1.greet === p2.greet check.
  • Mention private fields as the main thing classes give that plain prototypes do not.