by jdale27 · 14y ago · 0 comments
From what I remember the closure will keep a reference to el even though it will never use it, which means el won't be garbage collected when removed from the dom, which means the closure won't be removed from the onclick to free up el. Hence a circular reference. IE6's javascript garbage collector couldn't detect that.
The article is old. Here's an article from MS in 2005 describing the problem in more detail:
http://msdn.microsoft.com/en-us/library/Bb250448
Apologies if you understood that and were just saying the code is wrong, but it looks right to me. Not that I can be bothered to fire up IE6 to test it out.
EDIT: Some more detail about the definitely historical leak from SO, it also turns out it was only partially fixed in IE7:
http://stackoverflow.com/questions/1999840/javascript-circul...
Die, Explorer, die! :-)
It looks like my previous example was affected by eval.
The following reports foo's value as undefined while breaking for the debugger statement in Firefox and Chrome:
(function () {
var foo = "bar";
return function() {
debugger;
};
})()();
While the following reports foo's value as "bar" while breaking for the debugger statement: (function () {
var foo = "bar";
return function(_var) {
debugger;
return eval(_var);
};
})()('foo'); (function () {
var foo = "bar";
return function(_var) {
return eval(_var);
}
})()('foo');If you change the inner function call parameter 'foo' to ('alert("hi")') you'll see the reference to foo='bar' is still maintained in the inner function. So there is no -direct- reference to foo in the inner function, but there isn't even an -indirect- reference being made in the call to eval. The reference to foo is being maintained purely because eval "might" require it. Or so it appears.
If the return eval(_var) statement is replaced with alert('blah') we're back to foo being undefined in the inner function. So I've definitely got my finger pointed at eval on this one.