CKTIS / JavaScript
Object.setPrototypeOf — changing an object's prototype
For an ordinary object, a successful `Object.setPrototypeOf(obj, proto)` call sets its internal prototype without copying properties and returns the same object. `proto` must be an object or `null`. A `Proxy` with an extensible target can report success through its `setPrototypeOf` trap without changing the target's prototype. The call still returns the original `Proxy`.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
Object.setPrototypeOf(obj, proto)For an ordinary object, a successful Object.setPrototypeOf(obj, proto) call sets the internal [[Prototype]] of obj to proto. Inherited properties are looked up through the prototype chain rather than copied onto the object. The own properties of an ordinary object like the one in the example remain unchanged. If obj is a Proxy with an extensible target, its setPrototypeOf trap can report success without changing the target's prototype. The call still returns the original Proxy. The trap can also modify the target's own properties during a successful call. Calling Object.setPrototypeOf is not an assignment to a function's .prototype property.
proto must be an object or null; any other value causes a TypeError. Passing null or undefined as obj also causes a TypeError. Other primitive values are returned unchanged, but only after proto has been validated. For an object, a successful call returns exactly obj.
When changing an ordinary object's prototype, a detected cycle causes a TypeError. However, the cycle check stops at a prototype whose internal [[GetPrototypeOf]] method is not the ordinary implementation, such as a Proxy. A cycle through that Proxy can therefore go undetected without causing a TypeError. Attempting to change the prototype of a non-extensible ordinary object causes a TypeError. An ordinary object can be set to its existing prototype even if it is non-extensible. By contrast, a Proxy with a non-extensible target can reject the request through its setPrototypeOf trap even if the requested prototype is the target's current prototype. If the trap returns false, Object.setPrototypeOf throws a TypeError. Some objects, including Object.prototype, have an immutable prototype.
When an object's prototype is changed to null, the previous inheritance chain is disconnected; the object may then lose access to inherited methods. If the prototype is known when creating the object, consider Object.create(proto) instead of changing it later. In the example, role is inherited while name remains an own property.
Examples
const proto = { role: "reader" };
const user = { name: "Ada" };
const result = Object.setPrototypeOf(user, proto);
console.log(result === user);
console.log(Object.getPrototypeOf(user) === proto);
console.log(user.role);
console.log(Object.getOwnPropertyNames(user).join(","));
Object.setPrototypeOf(user, null);
console.log(user.role === undefined);Result
true true reader name true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.