handler.deleteProperty()
handler.deleteProperty()
方法是 [[Delete]]
对象内部方法 的陷阱,被 delete
运算符等操作使用。
¥The handler.deleteProperty()
method is a trap for the [[Delete]]
object internal method, which is used by operations such as the delete
operator.
Try it
语法
参数
返回值
描述
拦截
¥Interceptions
该陷阱可以拦截以下操作:
¥This trap can intercept these operations:
delete
运算符:delete proxy[foo]
和delete proxy.foo
Reflect.deleteProperty()
或调用 [[Delete]]
内部方法 的任何其他操作。
¥Or any other operation that invokes the [[Delete]]
internal method.
不变量
示例
捕获删除操作符
¥Trapping the delete operator
以下代码捕获 delete
运算符。
¥The following code traps the delete
operator.
js
const p = new Proxy(
{},
{
deleteProperty(target, prop) {
if (!(prop in target)) {
console.log(`property not found: ${prop}`);
return false;
}
delete target[prop];
console.log(`property removed: ${prop}`);
return true;
},
},
);
p.a = 10;
console.log("a" in p); // true
const result1 = delete p.a; // "property removed: a"
console.log(result1); // true
console.log("a" in p); // false
const result2 = delete p.a; // "property not found: a"
console.log(result2); // false
规范
Specification |
---|
ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-delete-p |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also