我有点搞不懂JavaScript的删除操作符。以下面这段代码为例:

var obj = {
    helloText: "Hello World!"
};

var foo = obj;

delete obj;

执行这段代码后,obj为空,但foo仍然引用与obj完全相同的对象。我猜这个对象就是foo指向的那个对象。

这让我很困惑,因为我以为写delete obj删除的是obj在内存中所指向的对象——而不仅仅是变量obj。

这是因为JavaScript的垃圾收集器是在保留/释放的基础上工作的,所以如果我没有任何其他变量指向对象,它将从内存中删除?

(顺便说一下,我的测试是在Safari 4中完成的。)


当前回答

ie5到ie8有一个错误,在主机对象(Window, Global, DOM等)的属性上使用delete会抛出TypeError“对象不支持此操作”。

var el=document.getElementById("anElementId");
el.foo = {bar:"baz"};
try{
    delete el.foo;
}catch(){
    //alert("Curses, drats and double double damn!");
    el.foo=undefined; // a work around
}

稍后,如果你需要检查属性在哪里具有完整的含义,请使用el。Foo !== undefined,因为“Foo”在el 在IE中总是返回true。

如果你真的想让房子消失……

function hostProxy(host){
    if(host===null || host===undefined) return host;
    if(!"_hostProxy" in host){
       host._hostproxy={_host:host,prototype:host};
    }
    return host._hostproxy;
}
var el=hostProxy(document.getElementById("anElementId"));
el.foo = {bar:"baz"};

delete el.foo; // removing property if a non-host object

如果你需要使用主机api的主机对象…

el.parent.removeChild(el._host);

其他回答

delete命令对常规变量无效,只对属性有效。在删除命令之后,属性没有值null,它根本不存在。

如果属性是对象引用,则delete命令删除属性,但不删除对象。如果该对象没有其他引用,则垃圾收集器将处理该对象。

例子:

var x = new Object();
x.y = 42;

alert(x.y); // shows '42'

delete x; // no effect
alert(x.y); // still shows '42'

delete x.y; // deletes the property
alert(x.y); // shows 'undefined'

(在Firefox中测试。)

来自Mozilla文档,“您可以使用delete操作符删除隐式声明的变量,但不能删除用var语句声明的变量。”

链接如下:https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Operators:Special_Operators:delete_Operator

delete操作符从数组中删除对象、对象的属性或元素。操作符还可以删除未使用var语句声明的变量。 在下面的例子中,'fruits'是一个声明为var的数组,并被删除了(真的吗??)

delete objectName
delete objectName.property
delete objectName[index] 
delete property // The command acts  only within a with statement.

var fruits = new Array("Orange", "Apple", "Banana", "Chery");
var newParagraph = document.createElement("p"); 
var newText = document.createTextNode("Fruits List : " + fruits);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
//Delete the array object.
delete fruits;
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Display the Fruits after delete the array object - Fruits List : "+ fruits;); 
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);

https://www.w3resource.com/javascript/operators/delete.php

Setting a variable to null makes sure to break any references to objects in all browsers including circular references being made between the DOM elements and Javascript scopes. By using delete command we are marking objects to be cleared on the next run of the Garbage collection, but if there are multiple variables referencing the same object, deleting a single variable WILL NOT free the object, it will just remove the linkage between that variable and the object. And on the next run of the Garbage collection, only the variable will be cleaned.

delete操作符只删除引用,从不删除对象本身。如果它确实删除了对象本身,其他剩余的引用将悬空,就像c++删除一样。(访问其中一个会导致崩溃。将它们全部变为null意味着在删除每个对象时需要额外的工作或额外的内存。)

因为Javascript是垃圾收集的,所以你不需要删除对象本身——当没有办法再引用它们时,它们就会被删除。

如果您已经完成了对对象的引用,那么删除它们可能会很有用,因为这为垃圾收集器提供了更多关于可以回收的内容的信息。如果对大对象的引用仍然存在,则可能导致该对象不可回收——即使程序的其余部分实际上并不使用该对象。