Implement the new Operator
new OperatorArguments
Constructor: Function —An ECMAScript constructable target to invoke with native construction semantics. args: any[] —Arguments to pass to the constructor.
Returns
object | Function — 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.
// 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); // => truePublic 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.
- Juejin: Shopee frontend interview record, two March 2020 loops
The seller-platform first round asked how the new operator is implemented (new 的实现), with code; the 2022 digital-bank round asked the same. This drill is a practice version of that task, not its wording.
Reported in a public Shopee interview account. Shopee frontend interview questions
Solution
Overview
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.
Approach: Delegate [[Construct]] to Reflect.construct
Call Reflect.construct(Constructor, args). Reflect.construct validates that Constructor is constructable, creates the correct receiver and prototype fallback, invokes classes and bound constructors, provides new.target, and applies native constructor return rules. A helper built from Object.create and apply is useful only as an explicitly restricted model for classic function constructors; it is not equivalent to native new. Complexity is O(k) to pass k arguments plus the constructor's own work.
export default function myNew(Constructor, ...args) {
return Reflect.construct(Constructor, args);
}
export default function myNew<T extends new (...args: any[]) => any>(
Constructor: T,
...args: ConstructorParameters<T>
): InstanceType<T> {
return Reflect.construct(Constructor, args) as InstanceType<T>;
}
Notes & Pitfalls
Calling Constructor.apply cannot invoke an ES class constructor and does not supply new.target. Checking only typeof Constructor === 'function' is insufficient because arrows and methods are callable but not constructable. Constructor return rule: objects and functions override the created instance; primitives are ignored. Reflect.construct throws TypeError for nonconstructable targets, matching native construction.
A constructor with null or primitive prototype falls back to Object.prototype. Bound constructable targets preserve their target's construction and instanceof behavior. A constructor may explicitly return an object or a function; either overrides the created receiver.
Use Reflect.construct to invoke the internal [[Construct]] operation. Let the runtime validate constructability and apply prototype fallback. Let native construction handle new.target and return overrides.
Common mistakes on this challenge
myNew is not delegating complete construction semantics
Classes, bound targets, new.target, prototype fallback, and return overrides require the internal [[Construct]] operation. Use Reflect.construct(Constructor, args)as the construction primitive.Let Reflect validate constructability and handle classes, bound targets, and new.target.Let native construction apply prototype fallback plus object/function return overrides.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.