javascript 在javascript中从内存中删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5115054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete object from memory in javascript
提问by Gerard
I am working on an AJAX application with a lot of Javascript. All pages are loaded through AJAX.
On a certain page I have a grid which is build in Javascript. Now when I leave that page I want to destroy that grid. I call jQuery.remove() but this only deletes the object from the DOM.
My question is how can I delete this grid object from the memory? Cause it still exists when I move away from the page.
我正在使用大量 Javascript 开发 AJAX 应用程序。所有页面都是通过 AJAX 加载的。
在某个页面上,我有一个用 Javascript 构建的网格。现在,当我离开那个页面时,我想销毁那个网格。我调用 jQuery.remove() 但这只会从 DOM 中删除对象。
我的问题是如何从内存中删除这个网格对象?因为当我离开页面时它仍然存在。
Much appreciated!
非常感激!
回答by krtek
If you delete all references to your grid (i.e. assign null to the variable), the garbage collector will delete the object from memory.
如果删除对网格的所有引用(即为变量分配 null),垃圾收集器将从内存中删除该对象。
回答by Trio Cheung
put the grid into a div or anything you want. when you want to delete it use
将网格放入 div 或任何你想要的。当你想删除它时使用
$("<the name of the div>").empty();
that will clear it.
这将清除它。

