var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value:", i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
The output is this:
My value: 3
My value: 3
My value: 3
However, I'd like it to produce:
My value: 0
My value: 1
My value: 2
The same issue arises when a delay in executing the function is due to event listeners:
var buttons = document.getElementsByTagName("button");
// let's create 3 functions
for (var i = 0; i < buttons.length; i++) {
// as event listeners
buttons[i].addEventListener("click", function() {
// each should log its value.
console.log("My value:", i);
});
<button>0</button>
<br />
<button>1</button>
<br />
<button>2</button>
… or using asynchronous code, such as with Promises:
// A function for async wait const wait = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms));
for (var i = 0; i < 3; i++) {
// Logi
once each promise resolves.
wait(i * 100).then(() => console.log(i));
This challenge can also be seen in for in
and for of
loops:
const arr = [1,2,3]; const fns = [];
for (var i in arr){
fns.push(() => console.log(“index:”, i));
}for (var v of arr){
fns.push(() => console.log(“value:”, v));
}for (const n of arr) {
var obj = { number: n }; // or new MyLibObject({ … })
fns.push(() => console.log(“n:”, n, “|”, “obj:”, JSON.stringify(obj)));
}
for(var f of fns){
f();
What is the fix for this common issue?