Well, the "problem" with closures in such a case is, that any access to i
would reference the same variable. That is because of ECMA-/Javascripts
function scope
or lexical scope
.
So to avoid that every call to alert(i);
would display a 5
(because after the loop finished i === 5), you need to create a new function which invokes itself at runtime.
To achieve this, you need to create a new function, plus you need the extra paranthesis at the end, to invoke the outer function
immediately, so link.onclick
has now the returned function as reference.