Given this function call:
var funcs = obj.getClosures([2, 4, 6, 8], function(x) {
return x*x;
});
I have the following function:
getClosures : function(arr, fn) {
var funcs = [];
var array = arr;
var i = 0;
var l = array.length;
(function(i, array) {
for (; i < l; i++) {
funcs[i] = function(i, array) {
return fn(array[i]);
};
}
}(i, array));
return funcs;
},
I'd like to be able to loop through the returned array and get the square root values of each item in the array exactly like this:
for (var i = 0; i < arr.length; i++) {
funcs[i]();
}
results each time through loop : 4, 16, 36, 64
Shouldn't my funcs array have a function reference in each index that can be readily invoked with the relevant argument values? Where did I go wrong?