Reflect.setPrototypeOf()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.
除了返回类型以外,静态方法 Reflect.setPrototypeOf() 与 Object.setPrototypeOf() 方法是一样的。它可设置对象的原型(即内部的 [[Prototype]] 属性)为另一个对象或 null,如果操作成功返回 true,否则返回 false。
尝试一下
const object1 = {};
console.log(Reflect.setPrototypeOf(object1, Object.prototype));
// Expected output: true
console.log(Reflect.setPrototypeOf(object1, null));
// Expected output: true
const object2 = {};
console.log(Reflect.setPrototypeOf(Object.freeze(object2), null));
// Expected output: false
语法
Reflect.setPrototypeOf(target, prototype)
参数
返回值
返回一个 Boolean 值表明是否原型已经成功设置。
异常
如果 target 不是 Object ,或 *prototype *既不是对象也不是 null,抛出一个 TypeError 异常。
描述
Reflect.setPrototypeOf 方法改变指定对象的原型(即,内部的 [[Prototype]] 属性值)。
示例
>使用 Reflect.setPrototypeOf()
js
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 |
|---|
| ECMAScript® 2026 Language Specification> # sec-reflect.setprototypeof> |
浏览器兼容性
Loading…