10 ms·
Section 7 is so completely wrong: var someVar = 'hello'; setTimeout((function() { alert(someVar); })(someVar), 1000); var someVar = 'goodbye'; Thi
by stanleydrew 15y ago
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.
- dexen 15y ago((EDIT: was irrelevant, as pointed out by stanleydrew in the post below. Thanks!))
- stanleydrew 15y agoThat'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.
- Mitya 15y ago@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.