Reflect.setPrototypeOf(target, prototype)changes the prototype of an object: the object that it inherits from. After the call, the object can use the properties and methods of its new prototype.
It returns true when the change worked and false when it was not allowed, for example on an object that is not extensible. The older Object.setPrototypeOf() throws an error in those cases.
Remember
Reflect.setPrototypeOf(target, prototype)
changed → true
not allowed → false (no error)
prototype must be an object or null
No loops: A cannot inherit from B if B inherits from A
Prefer Object.create() when you create objects
Syntax
Pass the object and the new prototype. The new prototype must be an object or null.
JavaScript
const canFly = { fly() { return "I can fly"; } };
const bird = { name: "Robin" };
console.log(Reflect.setPrototypeOf(bird, canFly)); // true
console.log(bird.fly()); // I can fly
Call
What it does
Reflect.setPrototypeOf(obj, proto)
Make proto the prototype of obj.
Reflect.setPrototypeOf(obj, null)
Remove the prototype completely.
Reflect.getPrototypeOf(obj)
Read the prototype again to check.
Object.setPrototypeOf(obj, proto)
The older version. It throws instead of returning false.
// ECMAScript 2015 (ES6)
Reflect.setPrototypeOf(target, prototype)
// target : an object
// prototype : an object or null
// returns : true or false
// throws : TypeError if target is not an object,
// or prototype is neither an object nor null
Parameters
Parameter
Description
target
Required. The object whose prototype you want to change. If it is not an object, a TypeError is thrown.
prototype
Required. The new prototype: an object, or null for no prototype. Any other value throws a TypeError.
Return Value
Return Value
Description
true
The prototype was changed (or it was already the same).
false
The change was not allowed: the object is not extensible, or the change would make a loop in the chain.
TypeError (thrown)
target is not an object, or prototype is not an object or null.
JavaScript Examples
Example 1 — Change the Prototype of an Object
Give a plain object the methods of another object by changing its prototype.
Before: function
Result: true
After: undefined
Prototype: null
Own data: 1
With undefined: TypeError
How It Works
Inherited by default. A plain object gets toString from Object.prototype, so the first line prints function.
Cut off. After setPrototypeOf(data, null), the object inherits nothing. toString is gone, but its own property a still works.
Not undefined. The new prototype must be an object or null. Passing undefined throws a TypeError.
Example 3 — When Reflect.setPrototypeOf() Returns false
Locked objects and loops in the chain cannot be changed. Reflect returns false, while Object.setPrototypeOf() throws. The catch block prints the error type only.
Locked object. A non-extensible object cannot get a new prototype, so Reflect returns false and Object.setPrototypeOf() throws a TypeError.
No loops.child already inherits from parent, so parent cannot inherit from child. The answer is false.
Same value is fine. Setting the prototype to the one it already has counts as a success.
Compatibility
Browser & Runtime Support
Reflect.setPrototypeOf() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.setPrototypeOf()
Supported in Chrome 49+, Edge 12+, Firefox 42+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox42+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
Reflect.setPrototypeOf()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It changes the prototype of an object, so the object inherits properties and methods from a different object. It returns true if it worked and false if it did not.
Both change the prototype. Reflect.setPrototypeOf() returns true or false. Object.setPrototypeOf() returns the object and throws a TypeError when the change is not allowed.
Yes. Reflect.setPrototypeOf(obj, null) removes the prototype, so the object does not inherit anything, not even toString(). But undefined is not allowed and throws a TypeError.
Usually not. It can slow your code down, and it makes the code harder to follow. Prefer Object.create(proto), a class with extends, or a Map or composition when you can.
A prototype chain must end at null. If A inherits from B, B cannot also inherit from A, because a property lookup would go around forever. JavaScript refuses this and returns false.