Skip to content
jshacks.dev
Fundamentals

JavaScript Interview Question Explain Prototypal Inheritance

How to answer the prototypal inheritance question: objects link to objects and lookup walks the chain. See shadowing, classes, shared array traps.

8 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, TypeScript, Node.js and npm logos.

When an interviewer asks about prototypal inheritance, they are checking one idea: that JavaScript objects inherit directly from other objects, with no copying and no classes underneath. A candidate who has only used class syntax often answers with the words of class-based languages, "the child class inherits the parent's methods", and misses that the mechanism is a chain of links followed at lookup time. The answer that works has three parts: every object has a hidden link to another object, reading a property walks that chain until it finds the name, and class is syntax built on the same links. This post gives the answer in thirty seconds, then shows the chain, constructor functions, classes, the read and write asymmetry that trips people up, the differences between prototype and __proto__, and the answers that lose the point. The examples run on Node.js 24.

The answer in thirty seconds

Every object has an internal link, called its prototype, to another object or to null. When you read a property, the engine looks at the object itself, and if the name is not there, it follows the link and looks at the prototype, then at the prototype's prototype, until it finds the name or reaches null. Inheritance is that lookup. Methods are shared, not copied: all instances created from the same prototype read the same function.

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

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

console.log(dog.speak());
console.log(Object.getPrototypeOf(dog) === animal);
console.log(Object.hasOwn(dog, "speak"), "speak" in dog);
console.log(Object.getOwnPropertyNames(dog));
Text
Rex makes a sound
true
false true
[ 'name' ]

dog has only one own property, name. The call dog.speak() found speak on animal, one step up the chain, and ran it with this set to dog, which is why the name is Rex. The in operator searches the whole chain and Object.hasOwn() checks only the object itself, so the two disagree for inherited names. Being able to write this snippet and explain those last two lines is what an interviewer is looking for.

Shadowing

An object's own property hides a property of the same name further up.

JavaScript
const base = { greet: () => "base hello", kind: "base" };
const child = Object.create(base);

console.log(child.greet(), child.kind);

child.greet = () => "child hello";
console.log(child.greet(), base.greet());

delete child.greet;
console.log(child.greet());
Text
base hello base
child hello base hello
base hello

Assigning to child.greet creates an own property and leaves base alone. The lookup now stops at the child. Deleting the own property uncovers the inherited one again, because nothing was ever overwritten. This is the clearest demonstration that the chain is live: change the prototype and every object that inherits from it changes too.

The chain for a plain object

JavaScript
const plain = {};

console.log(Object.getPrototypeOf(plain) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
console.log(typeof plain.toString, Object.hasOwn(plain, "toString"));

const list = [];
console.log(Object.getPrototypeOf(list) === Array.prototype);
console.log(Object.getPrototypeOf(Array.prototype) === Object.prototype);
Text
true
null
function false
true
true

Even {} has a chain: plain, then Object.prototype, then null. That is where toString and hasOwnProperty come from, and plain.toString is a function that is not an own property. An array's chain is one link longer: the array, then Array.prototype where map and push live, then Object.prototype. Every built-in type follows the same pattern, which is why a method defined on a prototype is available on all values of that type.

Constructor functions

Before class, a function and its prototype property were the way to build objects with shared methods.

JavaScript
function Person(name) {
  this.name = name;
}

Person.prototype.greet = function () {
  return `Hi, ${this.name}`;
};

const ada = new Person("Ada");
const grace = new Person("Grace");

console.log(ada.greet(), grace.greet());
console.log(ada.greet === grace.greet);
console.log(Object.getPrototypeOf(ada) === Person.prototype);
console.log(ada.constructor === Person);
console.log(Object.hasOwn(ada, "greet"));
Text
Hi, Ada Hi, Grace
true
true
true
false

A call with new creates an object, links it to Person.prototype, runs the function with this set to the object, and returns it. Each instance has its own name, and greet is a single function that both read through the chain. The constructor property that ada.constructor finds is also inherited: Person.prototype.constructor points back at the function.

Classes are the same mechanism

JavaScript
class Shape {
  describe() {
    return `a ${this.kind}`;
  }
}

class Circle extends Shape {
  kind = "circle";
}

const circle = new Circle();

console.log(circle.describe());
console.log(typeof Shape, Object.getOwnPropertyNames(Shape.prototype));
console.log(Object.getPrototypeOf(Circle.prototype) === Shape.prototype);
console.log(Object.getPrototypeOf(Circle) === Shape);
console.log(Object.getPrototypeOf(circle) === Circle.prototype);
Text
a circle
function [ 'constructor', 'describe' ]
true
true
true

A class is a function, and its methods are stored on its prototype, exactly as in the constructor function example. The extends keyword links Circle.prototype to Shape.prototype, and also links the constructors themselves, so static methods are inherited too: Object.getPrototypeOf(Circle) is Shape. The differences from the old style are small and real: class bodies are strict, methods are not enumerable, a class cannot be called without new, and a class declaration is in the temporal dead zone. The inheritance underneath is the same chain.

JavaScript
function Old() {}
Old.prototype.method = function () {};

class Modern {
  method() {}
}

const keys = (instance) => {
  const found = [];
  for (const key in instance) found.push(key);
  return found;
};

console.log(keys(new Old()));
console.log(keys(new Modern()));
Text
[ 'method' ]
[]

A method assigned to a function's prototype is enumerable, so for...in visits it, and a class method is not. That is one of the visible differences.

The read and write asymmetry

Reading walks the chain, and writing does not.

JavaScript
const defaults = { count: 1, tags: [] };
const first = Object.create(defaults);
const second = Object.create(defaults);

first.count++;
first.tags.push("shared");

console.log(first.count, second.count, defaults.count);
console.log(Object.hasOwn(first, "count"), Object.hasOwn(first, "tags"));
console.log(second.tags);
Text
2 1 1
true false
[ 'shared' ]

The expression first.count++ reads count through the chain, finds 1 on defaults, and writes 2 as an own property of first, leaving defaults at 1. The tags line behaves differently: first.tags finds the shared array and push changes that array in place, so second sees the new item. A mutable value on a prototype is shared between every instance, and that is the single most common prototype bug. Put arrays and objects in the constructor, so each instance gets its own.

Follow-up: prototype or __proto__?

These are different things with confusingly similar names.

NameWhat it is
Constructor.prototypeA property on a function: the object that becomes the prototype of instances made with new
object.__proto__An accessor on Object.prototype that reads or sets an object's actual prototype
Object.getPrototypeOf(object)The standard, supported way to read an object's prototype
[[Prototype]]The internal link the specification talks about
JavaScript
function Thing() {}
const thing = new Thing();

console.log(thing.__proto__ === Thing.prototype);
console.log(Object.getPrototypeOf(thing) === Thing.prototype);
console.log(Thing.__proto__ === Function.prototype);
console.log(typeof Thing.prototype, typeof thing.prototype);
Text
true
true
true
object undefined

Thing.prototype is an object, since every ordinary function has one. thing.prototype is undefined, because instances are not functions and have no such property. Their link is internal, and Object.getPrototypeOf() reads it. Prefer that function to __proto__ in new code. A pair of related functions set the link: Object.create(proto) creates an object with a chosen prototype, and Object.setPrototypeOf() changes it afterward, which engines handle poorly, so avoid it on hot code.

Objects with no prototype

JavaScript
const dictionary = Object.create(null);
dictionary.apple = 1;

console.log("toString" in dictionary, "toString" in {});
console.log(Object.getPrototypeOf(dictionary));
console.log(dictionary.hasOwnProperty);
Text
false true
null
undefined

An object created with Object.create(null) has an empty chain, so it inherits nothing, including toString. That makes it a safe dictionary for arbitrary keys, free of collisions with names like constructor. It also means the usual methods are missing, so use Object.hasOwn(dictionary, key) and not dictionary.hasOwnProperty(key). A Map is usually the better structure for key-value data.

Wrong answers that lose the point

  • "Prototypal inheritance copies properties from the parent." Nothing is copied: the object follows a link at lookup time, and a later change to the prototype is visible.

  • "JavaScript has no inheritance before ES6." It always had prototypes, and class was added as syntax over them.

  • "prototype and __proto__ are the same." One is a function's property for building instances, the other reads an object's link.

  • "A class is a different mechanism." It produces the same chain of prototype objects.

A mistake we hit: modifying a built-in prototype

JavaScript
Array.prototype.last = function () {
  return this[this.length - 1];
};

const numbers = [1, 2, 3];
console.log(numbers.last());

const seen = [];
for (const key in numbers) seen.push(key);
console.log(seen);

delete Array.prototype.last;
Text
3
[ '0', '1', '2', 'last' ]

Adding a method to Array.prototype made it available everywhere, and it also appeared as an enumerable key in every for...in loop over an array, because assigned properties are enumerable. Code that treated the loop keys as indices broke. We removed the method at the end of the example, since the change affects the whole program. A change to a built-in prototype also collides with future standard methods: a library that added Array.prototype.flat in its own way broke the day the language shipped its version. Use a helper function, or a subclass, instead.

Choosing between the techniques

You wantUse
An object that inherits from another objectObject.create(proto)
Instances with shared methodsA class
A dictionary with no inherited keysObject.create(null) or a Map
To check ownership of a propertyObject.hasOwn(object, key)
To read an object's prototypeObject.getPrototypeOf(object)
Per-instance arrays or objectsCreate them in the constructor

Practical rules we follow

Use class for objects with behavior, and Object.create when you want delegation between plain objects. Put methods on the prototype and data in the constructor. Never put a mutable value on a prototype. Do not modify built-in prototypes. Use Object.hasOwn() for own-property checks. Prefer composition to deep chains: a chain more than two or three levels long is hard to follow. And when you explain it in an interview, draw the chain.

Where the prototypal inheritance answer stops being enough

A deeper question asks about Symbol.species, Reflect.getPrototypeOf, mixins, super lookups through the home object, or the engine's hidden classes and inline caches. The answer above covers the model, and not the engine's optimizations, which are separate. The reference is in the MDN inheritance and the prototype chain guide and the ECMAScript specification.

Taggedinterview
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.