January 19 meeting notes

Axel Rauschmayer axel at rauschma.de
Fri Jan 20 07:29:34 PST 2012


> So any closures formed in the body of such a for(let...;...;...) loop will capture the binding created afresh for that iteration.
> 
> Thank you, Lord!  \o/    Can I get a Hallelujah?   \o/  
> 
> for (i=0; i < alerts.length; i++) {
>   setTimeout(function(i) { return function() { m.add(alerts[i]) } }(i), (i+1) * 100000);
> }
> 
> Here ^^ is a real loop I wrote last week, weeping the entire time.


I agree. If you are willing to add two more lines, you can make the code a little more readable:

    for (i=0; i < alerts.length; i++) {
        (function (i) {
            setTimeout(function() { m.add(alerts[i]) }, (i+1) * 100000);
        }(i));
    }

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120120/71981552/attachment-0001.html>


More information about the es-discuss mailing list