News for hackers. Stuff that matters.
Re:Oddities about JavaScript by stanleydrew on Monday May 30, 2011 @12:04PM (#2599707)
Section 7 is so completely wrong:

    var someVar = 'hello';
    setTimeout((function() { alert(someVar); })(someVar), 1000);
    var someVar = 'goodbye';
This won't do what he thinks it's going to do. Instead of alerting 'hello' after 1 second (after the value has been changed to 'goodbye') it alerts 'hello' immediately and does nothing after 1 second. Perhaps he meant something like:

    var someVar = 'hello';
    setTimeout(
        (function(innerVar) {
            return function() { alert(innerVar); };
        })(someVar),
        1000
    );
    var someVar = 'goodbye';
This should alert 'hello' after 1 second even though someVar has by then been overwritten.
Re:Oddities about JavaScript by Mitya on Tuesday May 31, 2011 @05:34AM (#2601970)
@stanleydrew - I changed this code late last night after I realised my mistake here. Of course I had meant to return a function from the SEF, which the code in the article now does. Apologies for the oversight.
Re:Oddities about JavaScript by dexen on Monday May 30, 2011 @12:39PM (#2599786)
((EDIT: was irrelevant, as pointed out by stanleydrew in the post below. Thanks!))
Re:Oddities about JavaScript by stanleydrew on Monday May 30, 2011 @12:44PM (#2599799)
That's the example of what doesn't work from the beginning of section 7. That will alert 'goodbye' after one second. I'm calling out his "fixed" code, which is supposed to alert 'hello' after one second.