Implement the new Operator

hard
By FrontendAtlas Editorial · Updated Oct 21, 2026
Implement myNew(Constructor, ...args) with the language's construction primitive. It must accept ECMAScript constructable targets, including classes and bound constructors, preserve new.target and native prototype fallback behavior, honor object or function return overrides, ignore primitive returns, and throw TypeError for nonconstructable targets.

Arguments

  • Constructor: Function — An ECMAScript constructable target to invoke with native construction semantics.
  • args: any[] — Arguments to pass to the constructor.

Returns

object | Function — The value produced by native construction, including any object or function returned explicitly by the constructor.

Solution idea

A hand-written Object.create + apply approximation cannot invoke classes, does not preserve new.target, mishandles bound constructors, and gives the wrong fallback when prototype is not an object. Reflect.construct performs the language's [[Construct]] operation and therefore supplies the intended native construction semantics for any constructable target.

Examples
// Example 1: normal constructor
function Person(name) { this.name = name; }
Person.prototype.say = function() { return `hi ${this.name}`; };

const p = myNew(Person, 'Ada');
p.name; // => 'Ada'
p.say(); // => 'hi Ada'
(p instanceof Person); // => true

// Example 2: constructor returns an object (must return that)
function Factory(name) {
  this.name = name;
  return { ok: true, name };
}

const f = myNew(Factory, 'Ada');
f; // => { ok: true, name: 'Ada' }

// Example 3: constructor returns primitive (ignore it)
function Weird(name) {
  this.name = name;
  return 123;
}

const w = myNew(Weird, 'Ada');
w.name; // => 'Ada'
(w instanceof Weird); // => true

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Shopee interview account. Shopee frontend interview questions