CodeZettaInterview Hub

JavaScript · Prototype · Mid-Level · Concept

What does the `new` operator do step by step?

Short Interview Answer

Creates an object, sets its prototype to `Fn.prototype`, binds `this` to that object while calling `Fn`, and returns the object (or an explicit object return).

Detailed Explanation

Rough algorithm: (1) create a new object; (2) set `[[Prototype]]` to `Constructor.prototype`; (3) invoke Constructor with `this` set to the new object and the given args; (4) if the constructor returns an object (or function), that value is the result of `new`; otherwise return the created object. Primitives returned from constructors are ignored. This is why constructors usually assign fields on `this` and put shared methods on `.prototype`. `new.target` lets constructors detect construct vs call.

Example

function Person(name) {
  this.name = name;
}
const p = new Person('Ada');
// equivalent conceptually to Object.create + call

Interview Tip

Mention the 'return object overrides `this`' rule — mid-level detail.

Common Mistake

Forgetting that returning a plain object from a constructor replaces the instance.

Did you know this answer?