by stanleydrew · 15y ago · 0 comments
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.