The handler.deleteProperty()
method is a trap for the [[Delete]]
object internal method, which is used by operations such as the delete
operator.
The handler.deleteProperty()
method is a trap for the [[Delete]]
object internal method, which is used by operations such as the delete
operator.
new Proxy(target, { deleteProperty(target, property) { } });
The deleteProperty()
method must return a boolean value indicating whether or not the property has been successfully deleted.
This trap can intercept these operations:
delete
operator: delete proxy[foo]
and delete proxy.foo
Reflect.deleteProperty()
Or any other operation that invokes the [[Delete]]
internal method.
If the following invariants are violated, the trap throws a TypeError
when invoked.
The following code traps the delete
operator.
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 |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
deleteProperty |
49 | 12 | 18 | 36 | 10 | 49 | 18 | 36 | 10 | 5.0 | 49 | 1.0 | 6.0.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/deleteProperty