Sjoerd Visscher's weblog
Last Update
10/20/2005; 10:56:34 PM
Thursday, October 20, 2005
Leak Free Javascript Closures
If you're confused about how closures in JavaScript cause memory leaks in Internet Explorer, this is for you: Leak Free Javascript Closures. Then, without leakage, you can write code like this:
function attach()
{
var element = document.getElementById("my-element");
element.attachEvent("onclick", function()
{
alert("Clicked: " + this.innerHTML);
}.closure(element));
}
Also if you want to do something like this: setTimeout(obj.method, 1000), and you find out that inside the method this is not obj, you can now do this: setTimeout(obj.method.closure(obj), 1000) to fix it.