Wrox Profesionnal Javascript p188
I don’t understand this part at line 8 where the anonymous function already have an argument of num, but how can (i) is copied into the argument num. Normally a function would stop at the closing bracket }, adding (i) doesn’t make sense for me.
function createFunctions(){
var result = new Array();
for (var i=0; i < 10; i++){
result[i] = function(num){
return function(){
return num;
};
}(i);
}
return result;
}
This is short hand for calling the function and passing it the parameter i. It’s like doing this:
Thinking of myFunction as the anonymous function my help you understand what’s going on, so instead of myFunction(i) you have function(num){…}(i);