JS Reflect.setPrototypeOf()
静态方法 Reflect``.setPrototypeOf() 与 Object.setPrototypeOf() 方法是一致的。它将指定对象的原型 (即,内部的[[Prototype]] 属性)设置为另一个对象或为 null。
语法
Reflect.setPrototypeOf(target, prototype)
参数
target
设置原型的目标对象。
prototype
对象的新原型 (一个对象或 null)。
返回值
返回一个 Boolean 值表明是否原型已经成功设置。
异常
抛出一个 TypeError 异常,如果目标不是 Object ,或原型不是一个对象或不为 null。
描述
Reflect.setPrototypeOf 方法改变指定对象的原型 (即,内部的 [[Prototype]] 属性值)。
示例
使用 Reflect.setPrototypeOf()
Reflect.setPrototypeOf({}, Object.prototype); // true
// It can change an object's [[Prototype]] to null.
Reflect.setPrototypeOf({}, null); // true
// Returns false if target is not extensible.
Reflect.setPrototypeOf(Object.freeze({}), null); // false
// Returns false if it cause a prototype chain cycle.
var target = {};
var proto = Object.create(target);
Reflect.setPrototypeOf(target, proto); // false
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262)Reflect.setPrototypeOf | Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262)Reflect.setPrototypeOf | Draft |