handler.construct(target, argumentsList, newTarget) is a trap that runs when you use the new keyword on a Proxy. It only works when the target of the Proxy is a constructor, such as a class or a normal function.
Inside the trap you can log the creation, change the arguments, or even return a different object. To create the object in the normal way, use Reflect.construct(target, argumentsList, newTarget). The trap must return an object.
Remember
const handler = {
construct(target, argumentsList, newTarget) { ... }
};
target → the original class or constructor
argumentsList → an array with the arguments
newTarget → the constructor that new was used on
return value → must be an object, or TypeError
Syntax
Wrap a class in a Proxy and add a construct trap to the handler.
JavaScript
class User {
constructor(name) {
this.name = name;
}
}
const TrackedUser = new Proxy(User, {
construct(target, args, newTarget) {
console.log("Creating " + args[0]);
return Reflect.construct(target, args, newTarget);
}
});
const ann = new TrackedUser("Ann"); // Creating Ann
Call
What it does
new proxy(1, 2)
Using new on the Proxy runs the trap.
Reflect.construct(proxy, [1, 2])
The function-style version. It runs the trap too.
class Child extends proxy {}
Extending a Proxy runs the trap when a Child is created.
proxy(1, 2)
A call without new does not run construct. It would use the apply trap.
// ECMAScript 2015 (ES6)
handler.construct(target, argumentsList, newTarget)
// target : the original constructor
// argumentsList : an array of the arguments
// newTarget : the constructor that new was called on
// returns : an object
// throws : TypeError if the result is not an object
Parameters
Parameter
Description
target
The original class or constructor function that the Proxy wraps.
argumentsList
A real array with the arguments that were passed to new. It is empty when there are none.
newTarget
The constructor that new was used on. It is the Proxy itself, unless a subclass is being created. Pass it on to Reflect.construct().
Return Value
Return Value
Description
Object
The new object. It becomes the result of new proxy(...). Usually this is the result of Reflect.construct().
TypeError (thrown)
The trap returned something that is not an object, such as a number or undefined.
JavaScript Examples
Example 1 — Log Every Object Creation
Wrap a class so that every new writes a note, and then creates the object in the normal way with Reflect.construct().
JavaScript
class User {
constructor(name) {
this.name = name;
}
}
const logs = [];
const TrackedUser = new Proxy(User, {
construct(target, args, newTarget) {
logs.push("new User(" + args.join(", ") + ")");
return Reflect.construct(target, args, newTarget);
}
});
const ann = new TrackedUser("Ann");
const bob = new TrackedUser("Bob");
console.log(ann.name + " and " + bob.name);
console.log("Log: " + logs.join(" | "));
console.log("instanceof User: " + (ann instanceof User));
console.log("instanceof TrackedUser: " + (ann instanceof TrackedUser));
Check each argument.typeof args[0] === "number" keeps real numbers and replaces everything else with 0.
Create with the fixed list.Reflect.construct(target, [x, y], newTarget) builds the object from the cleaned arguments, so a missing y becomes 0.
Plain class compared.new Point(5) has no trap, so y is undefined and JSON.stringify() leaves it out.
Example 3 — One Shared Object and Common Errors
The trap can return an object that already exists, which makes a singleton. Then see two mistakes that throw a TypeError. The catch blocks print the error type only.
JavaScript
class Settings {
constructor() {
this.theme = "light";
}
}
let instance = null;
const Single = new Proxy(Settings, {
construct(target, args, newTarget) {
if (instance === null) {
instance = Reflect.construct(target, args, newTarget);
}
return instance;
}
});
const a = new Single();
const b = new Single();
a.theme = "dark";
console.log("Same object: " + (a === b));
console.log("b.theme: " + b.theme);
const Bad = new Proxy(function () {}, {
construct() {
return 42;
}
});
try {
new Bad();
} catch (err) {
console.log("Returns a number: " + err.name);
}
const Arrow = new Proxy(() => 1, {
construct() {
return {};
}
});
try {
new Arrow();
} catch (err) {
console.log("Arrow function target: " + err.name);
}
Same object: true
b.theme: dark
Returns a number: TypeError
Arrow function target: TypeError
How It Works
Create once. The first new Single() builds a Settings object and saves it. Every later new returns that same object.
Shared on purpose.a and b are the same object, so changing the theme through a shows through b.
Two mistakes. A trap that returns 42 breaks the rule that it must return an object. An arrow function cannot be used with new, so the Proxy cannot either.
Compatibility
Browser & Runtime Support
handler.construct() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.construct()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.construct()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when you use new on a Proxy. It receives the original constructor, an array of arguments and newTarget, and it must return an object.
Use Reflect.construct(target, args, newTarget). It builds the object the normal way, including class fields and inheritance, and you can return the result from the trap.
Either the target is not a constructor (for example an arrow function), or your trap returned something that is not an object. Always return an object from construct.
No. A call without new uses the apply trap instead. For a class, calling it without new throws a TypeError anyway.
It is the constructor that new was used on. For new proxy() it is the Proxy itself. For a subclass, it is the subclass. Pass it to Reflect.construct() so the new object gets the right prototype.