Too much recursion 

Higher Order Javascript

 



   Subscribe in NewsGator Online    Subscribe in Rojo    Add to Google      Add to My AOL   Subscribe in FeedLounge   Add to netvibes  

Brendan Eich's Roadmap  Standard ECMA-262  www.schillmania.com  www.quirksmode.org 
www.snook.ca  www.davidflanagan.com  www.kryogenix.org  www.thomasfrank.se 
www.dustindiaz.com  Yahoo! UI Library  www.crockford.com  www.brainjacked.com 
chadlindstrom.ca  www.browserland.org     
       

Thursday, July 06, 2006

 

Javascript Closures for Dummies



Here is a great article by Morris Johns:

Javascript Closures for Dummies

Some of John's points:


Heres some good stuff on stopping IE memory leaks,(I wonder if IE7 will fix most of them, not to worry 'bout it though; if your building a big app or lots of closures and dom stuff, heres some ways to stop them:
if (window.document.all) window.onunload= ... ;) 
Simon Willison explains: "Closures make it easy to create a memory leak without meaning to. Consider this:
function addHandler() {
var el = document.getElementById('el');
el.onclick = function() {
this.style.backgroundColor = 'red';
}
}
The above code sets up the element to turn red when it is clicked. It also creates a memory leak. Why? Because the reference to el is inadvertently caught in the closure created for the anonymous inner function. This creates a circular reference between a JavaScript object (the function) and a native object (el). There are a number of workarounds for this problem. The simplest is this:
function addHandler() {
var el = document.getElementById('el');
el.onclick = function() {
this.style.backgroundColor = 'red';
}
el = null;
}
This works by breaking the circular reference. Surprisingly, one trick for breaking circular references introduced by a closure is to add another closure:
function addHandler() {
var clickHandler = function() {
this.style.backgroundColor = 'red';
}
(function() {
var el = document.getElementById('el');
el.onclick = clickHandler;
})();
}
The inner function is executed straight away, and hides its contents from the closure created with clickHandler."-©2006 A ReIntroduction to Javascript

Comments:
hmm.. im glad im not much of a javascript programmer, cuz it doesnt makes sense!
 
No, it definitely does not make sense at all! This lit. sucks!

Thx for the post though...
 
AGREED!!
 
Thanks so much for the post, really effective data.
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

June 2006   July 2006   August 2006   September 2006  

This page is powered by Blogger. Isn't yours?