first of all, there is a front-end classic closure interview question.
for(var i = 0;i<5;iPP){
setTimeout(()=>{
console.log(i)
},1000*i)
}
Everyone knows that the answer to this question is to type 5 every second.
then the interviewer asked the interviewer to output the correct number every 1 second, and you know to use closures:
function a(j){
setTimeout(()=>{
console.log(j)
},1000*j)
}
for(var i = 0;i<5;iPP){
a(i)
}
but the only way to do this is to use the function scope to save the value of I, so that the value of the variable found by the timer when performing the callback is the corresponding value of each loop, and the function of the closure is to give the outside of the function indirect access to the value inside the function.
so in a nutshell, I think this problem is not the function of closure, but just the function of function scope.
I don"t know if my understanding is correct.