handler.deleteProperty()
method is a trap for the
delete
operator.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
const p = new Proxy(target, {
deleteProperty: function(target, property) {
}
});
The following parameters are passed to the
deleteProperty()
方法。
this
is bound to the handler.
target
The target object.
property
Symbol
of the property to delete.
deleteProperty()
method must return a
布尔
indicating whether or not the property has been successfully deleted.
handler.deleteProperty()
method is a trap for the
delete
operator.
This trap can intercept these operations:
delete
proxy
[
foo
]
and
delete
proxy
.
foo
Reflect.deleteProperty()
If the following invariants are violated, the proxy will throw a
TypeError
:
The following code traps the
delete
operator.
const p = new Proxy({}, {
deleteProperty: function(target, prop) {
if (prop in target){
delete target[prop]
console.log('property removed: ' + prop)
return true
}
else {
console.log('property not found: ' + prop)
return false
}
}
})
let result
p.a = 10
console.log('a' in p) // true
result = delete p.a // "property removed: a"
console.log(result) // true
console.log('a' in p) // false
result = delete p.a // "property not found: a"
console.log(result) // false
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '[[Delete]]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
deleteProperty
|
Chrome 49 | Edge 12 | Firefox 18 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 18 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
Proxy
handler
delete
operator
Reflect.deleteProperty()