JavaScript · Lesson 16 of 20 · 4 min
Prototypal Inheritance & `new` Keyword
This is a classic filter question. Nailing the mechanics of new and the prototype chain separates senior candidates from the rest.
When an interviewer asks you to "explain prototypal inheritance," they aren't looking for a history lesson. They want to know if you understand how JavaScript objects actually get their properties and methods. They're testing your mental model for property lookups, this binding, and memory management. Get this right, and you prove you understand the language at a fundamental level.
What new actually does
The new keyword isn't magic. It's a four-step process. You need to be able to recite these steps on demand.
- Create a new, empty object. A blank slate,
{}. - Link the new object's
[[Prototype]]. This internal property is set to theprototypeproperty of the constructor function you called. This is the moment inheritance happens. - Execute the constructor function. The function is called with
thisbound to the new object from step 1. This is where properties likethis.name = nameget set on the new instance. - Return the new object. Unless the constructor explicitly returns its own, different object, the new object created in step 1 is returned automatically.
This sequence is non-negotiable. It's how every new Date() or new Map() call works under the hood.
Where to put your methods (and why)
The trap: Putting methods directly inside the constructor function.
// Don't do this.
function BadPerson(name) {
this.name = name;
this.greet = function() { // Creates a new function for every instance
console.log(`Hi, I'm ${this.name}`);
};
}
This works, but it's inefficient. Every new BadPerson() creates a brand new greet function in memory. If you create 1,000 instances, you create 1,000 separate greet functions.
The correct way is to place shared methods on the constructor's prototype property.
function Person(name) {
this.name = name;
}
// Do this instead.
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name}.`);
};
const alice = new Person('Alice');
const bob = new Person('Bob');
alice.greet(); // Hello, my name is Alice.
bob.greet(); // Hello, my name is Bob.
// Both instances share the *same* function object.
console.log(alice.greet === bob.greet); // true
Now, alice and bob don't have their own greet property. When you call alice.greet(), JavaScript doesn't find it on alice, so it looks up the prototype chain to Person.prototype and finds it there. This is memory efficient and the entire point of the pattern.
Untangling prototype, [[Prototype]], and __proto__
This is the most common point of confusion. Master this distinction.
-
Constructor.prototype: This is a property that exists on every function. It's just a plain object. Its purpose is to be the blueprint for instances created by that constructor. You attach shared methods to it. -
instance.[[Prototype]]: This is an internal, hidden property on every object. It's the actual link in the prototype chain. When you usenew Person(), the new object's[[Prototype]]is set to point toPerson.prototype. -
instance.__proto__: This is a non-standard, deprecated accessor for[[Prototype]]. You'll see it in browser consoles, and it's useful for debugging, but you shouldn't use it in production code. The standard way to read the prototype isObject.getPrototypeOf(instance).
An interviewer might ask: "What's the relationship between
alice,Person, andPerson.prototypeafter runningconst alice = new Person('Alice')?"Your answer should be: "
aliceis an object instance.Personis the constructor function used to create it. Thenewkeyword sets the internal[[Prototype]]ofaliceto be the same object as theprototypeproperty of thePersonfunction. Therefore,Object.getPrototypeOf(alice) === Person.prototype."
How to talk about the prototype chain
When you access a property like alice.toString(), the JavaScript engine performs a lookup.
- Does
alicehave its own property namedtoString? No. - Follow
alice.[[Prototype]]toPerson.prototype. DoesPerson.prototypehave atoString? No. - Follow
Person.prototype.[[Prototype]]toObject.prototype. DoesObject.prototypehave atoString? Yes. - Execute that function with
thisset toalice.
This chain of lookups is the essence of prototypal inheritance. The instanceof operator simply checks if a constructor's prototype object exists anywhere in an object's prototype chain. That's why alice instanceof Person and alice instanceof Object are both true.
What about Object.create()?
Object.create() is a more direct way to achieve prototypal inheritance. It creates a new object and sets its [[Prototype]] to the object you provide.
const obj = Object.create(somePrototype);
This is roughly equivalent to step 1 and 2 of the new keyword, but it doesn't run a constructor function (step 3). It's useful for creating objects that delegate to another object without the formality of a constructor. You'll see this in patterns that favor composition and delegation over classical-style inheritance. For most day-to-day work and in interviews, the constructor/new pattern is more common.
Try it
Write a function myNew that mimics the new keyword. It should accept a constructor function and any arguments for it. This is a common interview problem that forces you to implement the four steps yourself.
function myNew(Constructor, ...args) {
// 1. Create a new object.
// 2. Set its prototype.
// 3. Execute the constructor with the correct `this`.
// 4. Return the new object (handling the case where the constructor returns its own object).
}
Next: We'll look at the engine's to-do list and how it prioritizes work in the Event Loop, Microtasks & Macrotasks.
Lock it in — apply this lesson under interview conditions.