<HTML> <HEAD> <script language="javascript"> function
initpage() { window.setTimeout("window.location.reload()", 500,
"javascript"); } </script> </HEAD> <body onload="initpage()"
> <div class='menu' id='menu'></div> <script
language='javascript'> hookup(document.getElementById('menu')); function
hookup(element) { element.attachEvent( "onmouseover",
mouse); function mouse ()
{} } </script> </body> </HTML> In this code, the
handler (the mouse function) is nested inside the attacher (the hookup
function). This arrangement means that the handler is closed over the
scope of the caller (this arrangement is named a "closure").
function
AttachEvents(element) { // In order to remove this we need to
put // it somewhere. Creates another ref element.expandoClick =
ClickEventHandler;
// This structure causes element to ref
ClickEventHandler element.attachEvent("onclick",
element.expandoClick);
function ClickEventHandler() { //
This closure refs element } }
function
SetupLeak() { // The leak happens all at
once AttachEvents(document.getElementById("LeakedDiv")); }
不得不说Eric Lippert同志疾呼的:Don't use closures unless you really need
closure semantics. In most cases, non-nested functions are the right way to go.
是非常消极的应付之辞。今天关于IE内存泄漏的文章已有很多,而且很大部分就是微软的自己人在解释,甚至象Eric
Lippert这样引擎开发组的成员。但是他们的文章始终没有正面承认其实这就是IE的bug,而且是非常严重的bug,这事情其实完全不关脚本引擎对象和DOM对象的事。就是微软对产品不负责任的表现,不说IE4,1997那个春天那是太遥远了点,但是IE6也是2001年随xp发布的。使用COM可以给他们的开发带来很多便利,当然也利用很多现成的东西,可是居然在带来这样的严重问题后,他们却把大部分责任归咎于不合理和不正确的使用Closures技术!对于循环引用产生Memory
Leak我根本就是不好意思提了,那样的问题是应该发生的吗?那就让我们拭目以待看IE7怎么解决这堆shit问题吧,当然我希望不要再看到类似:Do not use
closures unless they are necessary. 这样的扯淡建议!
没有相关教程