Im has this closure code:
for (var i = 0, link; i < 5; i++) { link = document.createElement("a"); link.onclick = aaa(i); document.body.appendChild(link); } function aaa(num) { return function () { alert(num); }; } ;
I recently read a lot about closing.
There is one thing that I do not understand.
- When i == 0, it comes to aaa with i = 0 and is executed, returning a new function that should block the value
0 .
its fine (I understand it so far).
But what happens in i == 1?
- It returns to SAME aaa again, and now it must block the value
1 . OK
But wait! it already saves a βcloseβ for the value β0β!
Does this structure (closure) create new memory space for each iteration?
and if so, how can it be? we have only one centralized function aaa func!
source share