← Back to JavaScript, page 3

javascript-0045 · Hard senior

What does the new operator do, and how would you implement it yourself?

new creates an empty object linked to the constructor's prototype, runs the constructor with this set to that object, and returns the object, unless the constructor returns another object, which then replaces it. A hand-written version uses Object.create and apply, and Reflect.construct handles classes and new.target correctly.

Full answer

Calling a function with new performs four steps.

  1. Create a new empty object whose [[Prototype]] is Constructor.prototype.
  2. Call the constructor with this bound to that object, passing the arguments.
  3. If the constructor returns an object (or function), that value is the result of the new expression.
  4. Otherwise, the new object from step 1 is the result. Returning a primitive is ignored.

Implementing this takes a few lines: const obj = Object.create(Constructor.prototype); const result = Constructor.apply(obj, args); return result !== null && (typeof result === 'object' || typeof result === 'function') ? result : obj;.

This hand-written version has limits. ES6 classes throw a TypeError when called without new, so apply fails for them. Built-in constructors such as Date or Array create special internal objects that Object.create cannot produce. Also, new.target, the value that tells a function whether it was called with new and which constructor started the call, would be undefined.

Reflect.construct(Constructor, args, newTarget) does the real construction, including new.target and the base-class behavior of extends. Arrow functions and methods are not constructors, and new on them throws.

A common defensive pattern in old code was if (!(this instanceof Person)) return new Person(name). Modern code checks new.target instead, or uses classes, which enforce new.

javascript
function myNew(Constructor, ...args) {
  const instance = Object.create(Constructor.prototype);
  const result = Constructor.apply(instance, args);
  const isObject = result !== null && (typeof result === 'object' || typeof result === 'function');
  return isObject ? result : instance;
}

function Student(name) {
  this.name = name;
}
Student.prototype.hello = function () {
  return `I am ${this.name}`;
};

const viaNew = new Student('Ria');
const viaMyNew = myNew(Student, 'Dev');
console.log(viaNew.hello(), viaMyNew.hello(), viaMyNew instanceof Student);

function ReturnsObject() {
  this.ignored = true;
  return { replaced: true };
}
function ReturnsPrimitive() {
  this.kept = true;
  return 42;
}
console.log(new ReturnsObject(), new ReturnsPrimitive());
console.log(myNew(ReturnsObject), myNew(ReturnsPrimitive));

class Course {
  constructor(title) {
    this.title = title;
  }
}
try {
  myNew(Course, 'JS');
} catch (error) {
  console.log('myNew with class:', error.name);
}
console.log(Reflect.construct(Course, ['JS']) instanceof Course);

function Shape() {
  return new.target ? 'called with new' : 'called without new';
}
console.log(Shape(), new Shape() instanceof Shape);

const date = Reflect.construct(Date, [2026, 0, 15]);
console.log(date instanceof Date, date.getFullYear());
try {
  new (() => {})();
} catch (error) {
  console.log('arrow with new:', error.name);
}

Run with Node.js v24.22.1 as js-45.js; two runs gave identical output. Output: I am Ria I am Dev true { replaced: true } ReturnsPrimitive { kept: true } { replaced: true } ReturnsPrimitive { kept: true } myNew with class: TypeError true called without new true true 2026 arrow with new: TypeError

Key points

  • new creates an object linked to Constructor.prototype, runs the constructor on it, and returns it.
  • A constructor that returns an object overrides the result; a primitive return is ignored.
  • Classes cannot be called without new, so apply-based imitation fails for them.
  • Reflect.construct supports new.target and built-in subclassing.
  • Arrow functions and methods are not constructors.

Common mistakes

  • Forgetting that returning an object from a constructor replaces the new instance.
  • Linking the prototype with the instance's __proto__ assignment instead of Object.create or Object.setPrototypeOf.
  • Implementing new with apply and assuming it works for classes and built-ins.
  • Forgetting that new.target is undefined when a function is called normally.

Follow-ups

  • What does new.target return inside a derived class constructor?
  • Why must a derived class call super before using this?
  • What happens if Constructor.prototype is not an object when you call new?

Tips

  • List the four steps in order; interviewers usually ask for exactly that.
  • Write the implementation, then volunteer its two weaknesses (classes and built-ins).
  • Mention Reflect.construct as the faithful alternative.