Implement new Operator

Hard
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement new Operator

Approach

Replicate the four steps of the new operator: create a blank object with Object.create(Constructor.prototype), invoke the constructor with the new object as this via Constructor.apply(obj, args), and return the constructor result if it is an object, otherwise return the created object.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Constructor Pattern

Why It Works

The new operator links the created object to the constructor prototype chain before calling the constructor. If the constructor returns a non-object, the newly created object is used instead.

Updated Feb 2026