I'm a javascript newbie trying to wrap my mind around this code. I got it here http://brackets.clementng.me/post/24150213014/example-of-a-javascript-closure-settimeout-inside
I still have a hard time understanding it. Since it involves some pattern I'm not familiar with.
// output 0-9, seperated by 1 sec delay.
for (var i = 0; i < 10; i++) {
setTimeout(function(x) {
return function() {
console.log(x);
};
}(i), 1000*i);
}
What is the meaning of (i) in this section of code?
function(x) {
return function() {
console.log(x);
};
}(i)
I thought it is an immediately invoked function expression. But isn't the correct syntax for that is:
(function() {
// some code
})();