我正在尝试理解代码
for(var i = 0; i < 10; i++) {
setTimeout((function(e) {
return function() {
console.log(e);
}
})(i), 1000)
}
从这里http://bonsaiden.github.com/JavaScript-Garden/#function.closures
我理解这种方法:
for(var i = 0; i < 10; i++) {
(function(e) {
setTimeout(function() {
console.log(e);
}, 1000);
})(i);
}
谁能帮我解释一下第一个?
我将尝试解释我如何理解第一个,
first i is 0,
setTimeout is called,
self calling function "function(e)" is called with i=0,
Im stuck!! what happens when this function returns a function?