handler.setPrototypeOf()

handler.setPrototypeOf() 方法是对 [[SetPrototypeOf]] 对象内部方法 的陷阱,被 Object.setPrototypeOf() 等操作使用。

¥The handler.setPrototypeOf() method is a trap for the [[SetPrototypeOf]] object internal method, which is used by operations such as Object.setPrototypeOf().

Try it

语法

¥Syntax

js
new Proxy(target, {
  setPrototypeOf(target, prototype) {
  }
});

参数

¥Parameters

以下参数传递给 setPrototypeOf() 方法。this 绑定到处理程序。

¥The following parameters are passed to the setPrototypeOf() method. this is bound to the handler.

target

目标对象。

prototype

该对象的新原型或 null

返回值

¥Return value

如果 [[Prototype]] 更改成功,则 setPrototypeOf() 方法返回 true,否则返回 false

¥The setPrototypeOf() method returns true if the [[Prototype]] was successfully changed, otherwise false.

描述

¥Description

拦截

¥Interceptions

该陷阱可以拦截以下操作:

¥This trap can intercept these operations:

或调用 [[SetPrototypeOf]] 内部方法 的任何其他操作。

¥Or any other operation that invokes the [[SetPrototypeOf]] internal method.

不变量

¥Invariants

如果违反以下不变量,则陷阱在调用时会抛出 TypeError

¥If the following invariants are violated, the trap throws a TypeError when invoked.

  • 如果 target 不可扩展,则 prototype 参数必须与 Object.getPrototypeOf(target) 的值相同。

示例

¥Examples

如果你想禁止为对象设置新原型,则处理程序的 setPrototypeOf() 方法可以返回 false,也可以抛出异常。

¥If you want to disallow setting a new prototype for your object, your handler's setPrototypeOf() method can either return false, or it can throw an exception.

方法一:返回错误

¥Approach 1: Returning false

这种方法意味着任何在变异失败时抛出异常的变异操作都必须创建异常本身。

¥This approach means that any mutating operation that throws an exception on failure to mutate, must create the exception itself.

例如,Object.setPrototypeOf() 将创建并抛出 TypeError 本身。如果突变是由通常不会在失败时抛出的操作执行的,例如 Reflect.setPrototypeOf(),则不会抛出异常。

¥For example, Object.setPrototypeOf() will create and throw a TypeError itself. If the mutation is performed by an operation that doesn't ordinarily throw in case of failure, such as Reflect.setPrototypeOf(), no exception will be thrown.

js
const handlerReturnsFalse = {
  setPrototypeOf(target, newProto) {
    return false;
  },
};

const newProto = {},
  target = {};

const p1 = new Proxy(target, handlerReturnsFalse);
Object.setPrototypeOf(p1, newProto); // throws a TypeError
Reflect.setPrototypeOf(p1, newProto); // returns false

方法二:抛出异常

¥Approach 2: Throwing an Exception

后一种方法将导致任何尝试改变的操作被抛出。如果你甚至希望非抛出操作在失败时抛出,或者你希望抛出自定义异常值,则此方法是最佳选择。

¥The latter approach will cause any operation that attempts to mutate, to throw. This approach is best if you want even non-throwing operations to throw on failure, or you want to throw a custom exception value.

js
const handlerThrows = {
  setPrototypeOf(target, newProto) {
    throw new Error("custom error");
  },
};

const newProto = {},
  target = {};

const p2 = new Proxy(target, handlerThrows);
Object.setPrototypeOf(p2, newProto); // throws new Error("custom error")
Reflect.setPrototypeOf(p2, newProto); // throws new Error("custom error")

规范

Specification
ECMAScript Language Specification
# sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看