CKTIS / JavaScript
Reflect.setPrototypeOf — setting an object's prototype
Reflect.setPrototypeOf(target, proto) attempts to set an object's prototype and returns a boolean result. It distinguishes a rejected change from invalid arguments, which cause TypeError.
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
Reflect.setPrototypeOf(target, proto)Reflect.setPrototypeOf(target, proto) invokes the target object's internal [[SetPrototypeOf]] operation. The prototype is used to look up inherited properties; the method does not copy them onto the object. proto must be an object or null, which means no prototype. This does not change a function's .prototype property.
The result true means success, including when the requested prototype is already set. The result false means rejection. For an ordinary object, changing to a different prototype after Object.preventExtensions(target) returns false. Setting the same prototype again remains allowed even for a non-extensible object. When checking for a cycle for an ordinary object, the operation returns false if walking the chain from proto reaches target. However, the check stops at any object that does not use the ordinary [[GetPrototypeOf]] internal method. A Proxy in the chain can therefore allow a cycle to form even when target is an ordinary object.
A boolean result does not guarantee the absence of exceptions: a primitive target or an invalid proto causes TypeError. Errors from the internal operation, including errors from a Proxy trap, propagate. In contrast, for an object target, Object.setPrototypeOf returns the object on success and throws TypeError when the internal operation returns false.
The example demonstrates inheritance without copying and the difference between keeping and changing the prototype after preventing extensions. The language specification is not a browser compatibility table; using Node 24 in a repository does not establish browser support.
Examples
const proto = { label: "shared" };
const item = {};
console.log(Reflect.setPrototypeOf(item, proto));
console.log(item.label);
console.log(Object.hasOwn(item, "label"));
Object.preventExtensions(item);
console.log(Reflect.setPrototypeOf(item, proto));
console.log(Reflect.setPrototypeOf(item, null));
console.log(Object.getPrototypeOf(item) === proto);Result
true shared false true false 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.