本文翻译自:Deleting Objects in JavaScript
I'm a bit confused with JavaScript's delete
operator. 我对JavaScript的delete
操作符有点困惑。 Take the following piece of code: 采取以下代码:
var obj = {
helloText: "Hello World!"
};
var foo = obj;
delete obj;
After this piece of code has been executed, obj
is null
, but foo
still refers to an object exactly like obj
. 在执行了这段代码之后, obj
为null
,但是foo
仍然引用与obj
完全相同的obj
。 I'm guessing this object is the same object that foo
pointed to. 我猜这个对象与foo
指向的对象相同。
This confuses me, because I expected that writing delete obj
deleted the object that obj
was pointing to in memory—not just the variable obj
. 这让我感到困惑,因为我期望写delete obj
删除了obj
指向内存的对象 - 而不仅仅是变量obj
。
Is this because JavaScript's Garbage Collector is working on a retain/release basis, so that if I didn't have any other variables pointing to the object, it would be removed from memory? 这是因为JavaScript的垃圾回收工作在保留/释放的基础,所以,如果我没有指向对象的任何其他变量, 将它从内存中删除?
(By the way, my testing was done in Safari 4.) (顺便说一句,我的测试是在Safari 4中完成的。)
#1楼
参考:https://stackoom.com/question/37Bn/在JavaScript中删除对象
#2楼
based on @Guffa 's answer. 根据@Guffa的回答。 I found the following method works for me: 我发现以下方法适合我:
var obj = {
helloText: "Hello World!"
};
obj = null;
delete obj;
By setting the obj to null
first, you removed all the reference to it, then you can delete it completely. 通过首先将obj设置为null
,您删除了对它的所有引用,然后您可以完全删除它。
I didn't test it on other browser, but this works in phonegap 1.7.0 我没有在其他浏览器上测试它,但这适用于phonegap 1.7.0
#3楼
Aside from the GC questions, for performance one should consider the optimizations that the browser may be doing in the background -> 除了GC问题,对于性能,我们应该考虑浏览器可能在后台进行的优化 - >
http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/ http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/
It appears it may be better to null the reference than to delete it as that may change the behind-the-scenes 'class' Chrome uses. 看起来删除引用可能比删除它更好,因为这可能会改变Chrome使用的幕后“类”。
#4楼
I stumbled across this article in my search for this same answer. 我在寻找同样的答案时偶然发现了这篇文章。 What I ended up doing is just popping out obj.pop()
all the stored values/objects in my object so I could reuse the object. 我最终做的只是弹出obj.pop()
我对象中的所有存储值/对象,以便我可以重用该对象。 Not sure if this is bad practice or not. 不确定这是不好的做法。 This technique came in handy for me testing my code in Chrome Dev tools or FireFox Web Console. 这种技术对我在Chrome开发工具或FireFox Web控制台中测试代码非常方便。
#5楼
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. 将变量设置为null
可确保在所有浏览器中中断对对象的任何引用,包括在DOM元素和Javascript范围之间进行循环引用。 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. 通过使用delete
命令,我们在下一次运行Garbage集合时标记要清除的对象,但是如果有多个变量引用同一个对象,则删除单个变量将不释放该对象,它将只删除该变量之间的链接和对象。 And on the next run of the Garbage collection, only the variable will be cleaned. 并且在下一次运行垃圾收集时,只会清除变量。
#6楼
Just found a jsperf you may consider interesting in light of this matter. 刚刚发现了一个jsperf你可能会认为这件事很有趣。 (it could be handy to keep it around to complete the picture) (保持它完成图片可能很方便)
It compares delete , setting null and setting undefined . 它比较delete ,设置null和设置undefined 。
But keep in mind that it tests the case when you delete/set property many times. 但请记住,当您多次删除/设置属性时,它会测试这种情况。