JavaScript Proxy handler.setPrototypeOf() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Proxy trap

Definition

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.

JavaScript
const proxy = new Proxy({}, {
  setPrototypeOf(target, prototype) {
    console.log("Changing the prototype");
    return Reflect.setPrototypeOf(target, prototype);
  }
});

const animal = { speak() { return "..."; } };
Object.setPrototypeOf(proxy, animal);   // Changing the prototype
console.log(proxy.speak());             // ...
CallWhat it does
Object.setPrototypeOf(proxy, proto)Changes the prototype. Runs the trap.
Reflect.setPrototypeOf(proxy, proto)The function-style version. It returns true or false.
proxy.__proto__ = protoAssigning to __proto__ runs the trap too.
Object.getPrototypeOf(proxy)Does not run this trap. It uses getPrototypeOf.

A standard JavaScript Proxy trap (ES2015). You write it inside the handler object that you pass to new Proxy(). JavaScript calls it for you. No import required. Siblings: Proxy.revocable(), handler.apply(), handler.construct(), handler.defineProperty(), handler.deleteProperty(), handler.get(), handler.getOwnPropertyDescriptor(), handler.getPrototypeOf(), handler.has(), handler.isExtensible(), handler.ownKeys(), handler.preventExtensions(), handler.set().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.setPrototypeOf(target, prototype)

// target    : the original object
// prototype : an object or null
// returns   : true or false

Parameters

ParameterDescription
targetThe original object that the Proxy wraps.
prototypeThe new prototype: an object, or null for no prototype.

Return Value

Return ValueDescription
trueThe prototype was changed.
falseThe 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.

JavaScript
const logs = [];
const proxy = new Proxy({}, {
  setPrototypeOf(target, prototype) {
    logs.push(prototype === null ? "null" : "an object");
    return Reflect.setPrototypeOf(target, prototype);
  }
});

const animal = {
  speak() {
    return "...";
  }
};

console.log("Before: " + typeof proxy.speak);
Object.setPrototypeOf(proxy, animal);
console.log("After: " + proxy.speak());

console.log("Reflect result: " + Reflect.setPrototypeOf(proxy, null));
console.log("Without prototype: " + typeof proxy.speak);
console.log("Log: " + logs.join(", "));
Try It Yourself

How It Works

  1. Log, then change. The trap saves a short note and returns Reflect.setPrototypeOf(target, prototype), which really changes the prototype of the target.
  2. New methods. Before the change there is no speak method. After Object.setPrototypeOf() the Proxy inherits it from animal.
  3. Remove it again. Reflect.setPrototypeOf(proxy, null) also runs the trap, and returns true. The log shows both changes.

Example 2 — Block All Prototype Changes

Return false to keep the prototype as it is. See how Reflect, Object.setPrototypeOf() and __proto__ react. The catch blocks print the error type only.

JavaScript
const fixed = new Proxy({ a: 1 }, {
  setPrototypeOf() {
    return false;
  }
});

console.log("Reflect: " + Reflect.setPrototypeOf(fixed, {}));

try {
  Object.setPrototypeOf(fixed, {});
} catch (err) {
  console.log("Object.setPrototypeOf: " + err.name);
}

try {
  fixed.__proto__ = {};
} catch (err) {
  console.log("__proto__ assignment: " + err.name);
}

console.log("Prototype unchanged: " + (Object.getPrototypeOf(fixed) === Object.prototype));
Try It Yourself

How It Works

  1. Always refuse. The trap returns false and never changes anything.
  2. Three reactions. Reflect.setPrototypeOf() reports the refusal as false. Object.setPrototypeOf() and the __proto__ assignment both throw a TypeError.
  3. 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.

JavaScript
const allowed = [Array.prototype, Object.prototype];

const picky = new Proxy({}, {
  setPrototypeOf(target, prototype) {
    if (!allowed.includes(prototype)) {
      return false;
    }
    return Reflect.setPrototypeOf(target, prototype);
  }
});

console.log("Array.prototype: " + Reflect.setPrototypeOf(picky, Array.prototype));
console.log("Date.prototype: " + Reflect.setPrototypeOf(picky, Date.prototype));
console.log("Now inherits from Array: " + (Object.getPrototypeOf(picky) === Array.prototype));

const locked = Object.preventExtensions({});
const liar = new Proxy(locked, {
  setPrototypeOf() {
    return true;
  }
});
try {
  Object.setPrototypeOf(liar, Array.prototype);
} catch (err) {
  console.log("Changing a locked target: " + err.name);
}
Try It Yourself

How It Works

  1. A list of allowed values. allowed.includes(prototype) checks the new prototype. Only Array.prototype and Object.prototype pass.
  2. 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.
  3. Locked targets are fixed. The target locked cannot get a new prototype. A trap that approves the change anyway makes JavaScript throw a TypeError.

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.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 49+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 36+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 49+ · Chromium based
Supported
Node.js 6+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer No 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.

Back to the Method Index

Return to the list of all Proxy methods and pick the next one to learn.

All Proxy methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful