Reflect.setPrototypeOf()
The Reflect.setPrototypeOf() method sets the prototype of an object.
It is similar to Object.setPrototypeOf() method.
When to Use Reflect?
| Case | Use | Why |
|---|---|---|
| Getting / setting values | Yes | Consistent return values |
| Creating objects like new | Yes | Reflect.construct() works with Proxy |
| Calling a function with context | Yes | Reflect.apply() is cleaner than func.apply() |
| Meta programming | Yes | Designed for low-level tasks |
| Simple object work | No | Use normal JS syntax |
Syntax
Reflect.setPrototypeOf(obj, prototype)
Parameters
| Parameter | Description |
|---|---|
| obj | Required. The target object. |
| prototype | Required. The prototype to set. |
Return Value
| Type | Description |
|---|---|
| Boolean |
true if successfull. false if not. |
Errors
| Type | Description |
|---|---|
| TypeError | Thrown if obj is not an object. |
Reflect Methods:
Reflect.apply()
Reflect.construct()
Reflect.defineProperty()
Reflect.deleteProperty()
Reflect.get()
Reflect.getOwnPropertyDescriptor()
Reflect.getPrototypeOf()
Reflect.has()
Reflect.isExtensible()
Reflect.ownKeys()
Reflect.preventExtensions()
Reflect.set()
Reflect.setPrototypeOf()
Browser Support
Reflect.setPrototypeOf() is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
| Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |