📜  JavaScript handler deleteProperty()方法

📅  最后修改于: 2020-10-25 09:14:10             🧑  作者: Mango

JavaScript handler.deleteProperty()方法

用于完全使用delete运算符删除属性的handler.deleteProperty()方法。如果删除成功,则此方法返回true。

句法

deleteProperty: function(target, property)

参量

目标:目标对象。

属性:要删除的属性的名称。

返回值

此方法返回一个布尔值。它指示属性是否成功删除。

浏览器支持

Chrome 49
Edge 12
Firefox 18
Opera 36

例子1

var proxy = new Proxy({}, {
  deleteProperty: function(target, prop) {
    document.writeln("Called: " + prop);
    return true;
    //if sucessfullt delete,return true.
  }
});delete proxy.abc;

输出:

Called: abc

例子2

var proxy = new Proxy({}, {
  deleteProperty: function(target, name) {
    document.write('In delete Property  ');
 
    return delete target[name];
  }
});
delete proxy.foo;
document.writeln(proxy.name);

输出:

In delete Property undefined 

例子3

var f = { bar: 'baz' }
f.bar = 'baz'
document.writeln('bar' in f)

delete f.bar
document.writeln('bar' in f)

var foo = { bar: 'baz' }
foo.bar = 'baz'
document.writeln('bar' in foo)

输出:

true false true