The handler.setPrototypeOf() method is a trap for Object.setPrototypeOf().


var p = new Proxy(target, {
  setPrototypeOf: function(target, prototype) {


The following parameters are passed to the setPrototypeOf method. this is bound to the handler.

The target object.
The object's new prototype or null.

Return value

The setPrototypeOf method returns true if the [[Prototype]] was successfully changed, otherwise false.


The handler.setPrototypeOf method is a trap for Object.setPrototypeOf().


This trap can intercept these operations:


If the following invariants are violated, the proxy will throw a TypeError:

  • If target is not extensible, the prototype parameter must be the same value as Object.getPrototypeOf(target).


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.

The former approach means that any operation that performs such mutation, that throws an exception on failure to mutate, will have to 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.

var handlerReturnsFalse = {
    setPrototypeOf(target, newProto) {
        return false;

var newProto = {}, target = {};

var p1 = new Proxy(target, handlerReturnsFalse);
Object.setPrototypeOf(p1, newProto); // throws a TypeError
Reflect.setPrototypeOf(p1, newProto); // returns false

The latter approach will cause any operation that attempts to mutate, to throw.  This approach is required if you want even non-throwing operations to throw on failure, or you want to throw a custom exception value.

var handlerThrows = {
    setPrototypeOf(target, newProto) {
        throw new Error('custom error');

var newProto = {}, target = {};

var p2 = new Proxy(target, handlerThrows);
Object.setPrototypeOf(p2, newProto); // throws new Error("custom error")
Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of '[[SetPrototypeOf]]' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of '[[SetPrototypeOf]]' in that specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
setPrototypeOfChrome Full support 49Edge ? Firefox Full support 49IE No support NoOpera Full support 36Safari ? WebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 49Opera Android Full support 36Safari iOS ? Samsung Internet Android Full support Yesnodejs Full support 6.0.0


Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also