What is the best (or good) practise to store a value in a function variable before the function is added to a function array?
For example, we have a counter:
var f_counter = 0;
... and we have a function array:
var a_func = [];
When we add a function to the array, we can do:
a_func.push(
function(){
examplecallbackfunction(f_counter);
}
);
f_counter++;
Here is the example callback function:
function examplecallbackfunction(c) {
<... code ...>
}
Loop through and execute the function array:
var l = a_func.length;
while (l>0) {
var fnc=a_func[l-1];
fnc();
l--;
}
The problem here is that when executing the function array the argument f_counter is what the current global variable f_counter, and not what f_counter was when the individual functions was added to the function array.
I need a good practise storing the current value of f_counter inside of the function definition before it is assigned to the function array, and when iterating and executing the functions in the array it should for example call the callback function using the value stored.
I need a way to do this without having the individual values stored in global variables because I load stuff async depending on user interaction so I never know "in advance" how many and what type of functions that will be stored in the function array. Some functions could have more variables than f_counter, and so on ...