The handler.setPrototypeOf()
method is a trap for Object.setPrototypeOf()
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
const p = new Proxy(target, { setPrototypeOf: function(target, prototype) { } });
Parameters
The following parameters are passed to the setPrototypeOf()
method. this
is bound to the handler.
target
- The target object.
prototype
- The object's new prototype or
null
.
Return value
The setPrototypeOf()
method returns true
if the [[Prototype]]
was successfully changed, otherwise false
.
Description
The handler.setPrototypeOf()
method is a trap for Object.setPrototypeOf()
.
Interceptions
This trap can intercept these operations:
Invariants
If the following invariants are violated, the proxy will throw a TypeError
:
- If
target
is not extensible, theprototype
parameter must be the same value asObject.getPrototypeOf(target)
.
Examples
If you want to disallow setting a new prototype for your object, your handler's setPrototypeOf()
method can either return false
, or it can throw an exception.
Approach 1: Returning false
This approach means that any mutating operation that throws an exception on failure to mutate, must create the exception itself.
For example, Object.setPrototypeOf()
will create and throw a TypeError
itself. If the mutation is performed by an operation that doesn't ordinarily throw in case of failure, such as Reflect.setPrototypeOf()
, no exception will be thrown.
const handlerReturnsFalse = { setPrototypeOf(target, newProto) { return false; } }; const newProto = {}, target = {}; const p1 = new Proxy(target, handlerReturnsFalse); Object.setPrototypeOf(p1, newProto); // throws a TypeError Reflect.setPrototypeOf(p1, newProto); // returns false
Approach 2: Throwing an Exception
The latter approach will cause any operation that attempts to mutate, to throw. This approach is best if you want even non-throwing operations to throw on failure, or you want to throw a custom exception value.
const handlerThrows = { setPrototypeOf(target, newProto) { throw new Error('custom error'); } }; const newProto = {}, target = {}; const p2 = new Proxy(target, handlerThrows); Object.setPrototypeOf(p2, newProto); // throws new Error("custom error") Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of '[[SetPrototypeOf]]' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
setPrototypeOf | Chrome Full support 49 | Edge Full support 12 | Firefox Full support 49 | IE No support No | Opera Full support 36 | Safari Full support 10 | WebView Android Full support 49 | Chrome Android Full support 49 | Firefox Android Full support 49 | Opera Android Full support 36 | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support