The setPrototypeOf(target, prototype) method

The setPrototypeOf trap is executed when we set the value of the internal [[prototype]] property, using either the Object.setPrototypeOf() method or the __proto__ property. It takes two parameters--that is, the target object and value of the property to be assigned.

This trap will return a Boolean, indicating whether it has successfully set the prototype or not.

Here is a code example that demonstrates how to use the setPrototypeOf trap:

const proxy = new Proxy({}, {     setPrototypeOf(target, value) {         Reflect.setPrototypeOf(target, value);         return true;     } }); Reflect.setPrototypeOf(proxy, {name: "Eden"}); console.log(Reflect.getPrototypeOf(proxy).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.