javascript 如何销毁javascript中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11292909/
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
How to destroy an object in javascript?
提问by Philip Murphy
I'm wondering how to properly destroy an object in Javascript that contains an array among other data.
我想知道如何在 Javascript 中正确销毁一个包含其他数据数组的对象。
For example (this is the initial state of the object):
例如(这是对象的初始状态):
acme.models.nomination = {
recipients : [], // array will be added to later.
awardType: {}, // this object will be filled out later.
privacy: 'private',
...
};
As the application is used the nomination.recipients object will be added to, along with the other data elements. Is it enough to do the following to clear the complete object when finished with it:
随着应用程序的使用,nomion.recipients 对象将与其他数据元素一起添加到其中。完成以下操作是否足以清除整个对象:
acme.models.nomination = {};
or is this better (or overkill):
或者这更好(或矫枉过正):
acme.models.nomination.privacy = undefined;
acme.models.nomination.awardType = {};
acme.models.nomination.recipients = {};
acme.models.nomination = {};
I'm thinking that the former statement (i.e. acme.models.nomination = {}
) is enough as this effectively makes the entire contents unreachable and hence eligible for garbage collection. However, I'd like to get other peoples opinions on this. BTW, the answer can be given in a modern browser context, let's say browsers post-Jan-2012, as I know memory management was a bit inconsistent in the past.
我认为前一个语句 (ie acme.models.nomination = {}
) 就足够了,因为这有效地使整个内容无法访问,因此有资格进行垃圾收集。但是,我想了解其他人对此的看法。顺便说一句,答案可以在现代浏览器上下文中给出,假设是 2012 年 1 月之后的浏览器,因为我知道内存管理在过去有点不一致。