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
语法
参数
返回值
描述
拦截
¥Interceptions
该陷阱可以拦截以下操作:
¥This trap can intercept these operations:
或调用 [[SetPrototypeOf]]
内部方法 的任何其他操作。
¥Or any other operation that invokes the [[SetPrototypeOf]]
internal method.
不变量
示例
方法一:返回错误
¥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.
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.
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 |
浏览器兼容性
BCD tables only load in the browser