I am learning closures. I understand the javascript module pattern, but it seems like the only other "practical application" of closures is to avoid problems with functions being created in a loop and pushed to an array.
I can't think of a single reason to do this. Here is an example of a closure issue in a loop taken from here: JavaScript closure inside loops – simple practical example
var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
funcs[i] = function() { // and store them in funcs
console.log("My value: " + i); // each should log its value.
};
}
for (var j = 0; j < 3; j++) {
funcs[j](); // and now let's run each one to see
}
When would you ever do this in real software? I am at a loss. I wrote a factoryand it works fine, so I am really failing to see a real world applications here.