为什么在document.getElementById-ed之后,为什么对象必须被空为IE? [英] Why object has to be nulled for IE after it was document.getElementById-ed?

查看:170
本文介绍了为什么在document.getElementById-ed之后,为什么对象必须被空为IE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在第三方JavaScript代码中看到:

I often see in third party JavaScript code that after:

var el = document.getElementById(elementId);

对象通常被置零,并且沿着这个操作发表评论说它是为IE完成的:

object is often nulled and comment along this operation says that it is done for IE:

el = null; // IE

什么是真正的目的?任何资源?

What's the real purpose? Any resource on that?

推荐答案

通过定义引用,它们会破坏DOM对象和JavaScript对象之间的相应的循环依赖关系,由较旧的IE中的不同子系统控制(因此无法进行垃圾收集)。

By nixing a reference they break the corresponding cyclic dependency between the DOM object and JavaScript objects, which are controlled by different sub-systems in older IE (thus being impossible to be garbage-collected).

例如:

var el = document.getElementById(elementId);
el.onclick = function () { // here the cyclic reference is created
    /...
};

JavaScript子系统现在已经引用了 el 元素,并且DOM子系统( el 元素)引用了JavaScript对象(函数加上它关闭的内容)。

The JavaScript subsystem has now a reference to the el element, and the DOM subsystem (the el element) has a reference to the JavaScript object (the function plus what it closes in).

不用担心,如果您通过 addEventListener 添加监听器。

You don't have to worry, though, if you add the listeners via addEventListener.

要了解有关常见内存泄漏漏洞的更多信息,请参见 http:// www.ibm.com/developerworks/cn/web/library/wa-memleak/

To read more about common memory leak pitfalls, see http://www.ibm.com/developerworks/web/library/wa-memleak/.

这篇关于为什么在document.getElementById-ed之后,为什么对象必须被空为IE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆