Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
JavaScript for Frontend Interviews
  1. 1.Introduction to JavaScript & Setup
  2. 2.Variables and Data Types
  3. 3.Operators and Expressions
  4. 4.Control Flow: Conditionals
  5. 5.Control Flow: Loops
  6. 6.Functions: Basics
  7. 7.Arrays and Objects
  8. 8.Higher-Order Functions & Callbacks
  9. 9.Scope and Closures
  10. 10.`this` Keyword and Context
  11. 11.Asynchronous JavaScript: Callbacks & Promises
  12. 12.ES6+ Features (Part 1)
  13. 13.ES6+ Features (Part 2)
  14. 14.Error Handling & Debugging
  15. 15.Asynchronous JavaScript: Async/Await
  16. 16.Prototypal Inheritance & `new` Keyword
  17. 17.Event Loop, Microtasks & Macrotasks
  18. 18.Working with the DOM
  19. 19.Advanced Array Methods & Data Structures
  20. 20.Frontend Interview Prep & Best Practices
JavaScript for Frontend Interviews
16

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.

  1. Create a new, empty object. A blank slate, {}.
  2. Link the new object's [[Prototype]]. This internal property is set to the prototype property of the constructor function you called. This is the moment inheritance happens.
  3. Execute the constructor function. The function is called with this bound to the new object from step 1. This is where properties like this.name = name get set on the new instance.
  4. 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 use new Person(), the new object's [[Prototype]] is set to point to Person.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 is Object.getPrototypeOf(instance).

An interviewer might ask: "What's the relationship between alice, Person, and Person.prototype after running const alice = new Person('Alice')?"

Your answer should be: "alice is an object instance. Person is the constructor function used to create it. The new keyword sets the internal [[Prototype]] of alice to be the same object as the prototype property of the Person function. 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.

  1. Does alice have its own property named toString? No.
  2. Follow alice.[[Prototype]] to Person.prototype. Does Person.prototype have a toString? No.
  3. Follow Person.prototype.[[Prototype]] to Object.prototype. Does Object.prototype have a toString? Yes.
  4. Execute that function with this set to alice.

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.

Try an AI mock interview
Previous

Asynchronous JavaScript: Async/Await

Next

Event Loop, Microtasks & Macrotasks