How do JavaScript closures work?



you can find a lot of resources to answer your question, but... just in case you're just too lazy to search google for it, here's your answer. :)
 
1. Think of more relevant names to your functions. I can't tell you how many times I've seen function#n.
2. I took out the second reference to function1(); and I also took out function3 since it had no relevance.
3. I made a reference to function1 inside the event listener so it will redo the function and increment idImag again.

Leomaster
 
In other words, a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. To use a closure, simply define a function inside another function and expose it.
 
What allahbarkatlock said. But also you should google up on javascript's weird scoping as it catches people out often with closures. Variables are only created at function level/scope. If you create a variable in a for loop it will scope the variable on the parent, and when your async closure gets around to reading the variable it will be stale data and not what you expect.
 
A closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. The inner function will have access to the variables in the outer function scope, even after the outer function has returned
 
Graphic design, software development, website construction, small program development to find EPWK
 
Graphic design, software development, website construction, small program development to find EPWK