handler.setPrototypeOf(target, prototype) is a trap that runs when someone changes the prototype of a Proxy. This happens with Object.setPrototypeOf(), Reflect.setPrototypeOf() and with an assignment to __proto__.
The trap returns true when the prototype was changed and false when it was not. Inside the trap you can log the change, allow only some prototypes, or block it. To change the prototype in the normal way, use Reflect.setPrototypeOf(target, prototype).
Remember
const handler = {
setPrototypeOf(target, prototype) { ... }
};
target → the original object
prototype → the new prototype: an object or null
return true → the prototype was changed
return false → refused (Object.setPrototypeOf throws)
Syntax
Add a setPrototypeOf trap to the handler of a Proxy.
// ECMAScript 2015 (ES6)
handler.setPrototypeOf(target, prototype)
// target : the original object
// prototype : an object or null
// returns : true or false
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
prototype
The new prototype: an object, or null for no prototype.
Return Value
Return Value
Description
true
The prototype was changed.
false
The change is refused. Reflect.setPrototypeOf() returns false, while Object.setPrototypeOf() and an assignment to __proto__ throw a TypeError.
TypeError (thrown)
The trap said true for a target that is not extensible, but the new prototype is not the current one.
JavaScript Examples
Example 1 — Log Every Prototype Change
Write a note for each change, and then change the prototype of the target for real. The Proxy then uses the methods of the new prototype.
Always refuse. The trap returns false and never changes anything.
Three reactions.Reflect.setPrototypeOf() reports the refusal as false. Object.setPrototypeOf() and the __proto__ assignment both throw a TypeError.
Nothing changed. The prototype is still Object.prototype, which the last line confirms.
Example 3 — Allow Only Some Prototypes and Follow the Rules
The trap can keep a list of allowed prototypes. Then see that a trap cannot approve a change for a locked target. The catch block prints the error type only.
Array.prototype: true
Date.prototype: false
Now inherits from Array: true
Changing a locked target: TypeError
How It Works
A list of allowed values.allowed.includes(prototype) checks the new prototype. Only Array.prototype and Object.prototype pass.
Allowed, then refused. The first change works. Date.prototype is not in the list, so the trap returns false and the prototype stays Array.prototype.
Locked targets are fixed. The target locked cannot get a new prototype. A trap that approves the change anyway makes JavaScript throw a TypeError.
Compatibility
Browser & Runtime Support
handler.setPrototypeOf() 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.setPrototypeOf()
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.setPrototypeOf()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 the prototype of a Proxy is changed, with Object.setPrototypeOf(), Reflect.setPrototypeOf() or an assignment to __proto__. It returns true or false.
The prototype is not changed. Reflect.setPrototypeOf() returns false, while Object.setPrototypeOf() and an assignment to __proto__ throw a TypeError.
No. It only gets the request. To really change the prototype, call Reflect.setPrototypeOf(target, prototype) inside the trap and return its result.
No. Object.create(proto) makes a new object with that prototype, and no existing Proxy is changed. The trap only runs when the prototype of an existing Proxy is changed.
It can slow down code and make it harder to follow. A setPrototypeOf trap lets you guard against it, for example by blocking changes or allowing only a short list of safe prototypes.