The construct(target, arguments) method

If the target is a function, then calling the target as a constructor using the new operator or the Reflect.construct() method will execute the construct trap.

The construct trap takes two parameters. The first parameter is the target object and the second parameter is an array, representing the arguments of the constructor call.

The construct trap must return an object, representing the newly created instance. Here is a code example that demonstrates how to use the construct trap:

const proxy = new Proxy(function(){}, {     construct(target, arguments) {         return {name: arguments[0]};     } }); const obj = new proxy("Eden"); console.log(obj.name); //Output "Eden"

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.