I had this code:
function (i) { alert(i); }(3);
And it didn’t work, so after a StackOverFlow question - I changed it to:
(function(i){ alert(i); })(3);
And it works.
I had to () wrap all the code.
But then I saw this code on another site:
function addLinks () { for (var i=0, link; i<5; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (num) { return function () { alert(num); }; }(i);
I wanted to ask , what is the role for part (i) ? Does this do something?
And if it does. Why is it not in the template:
(function(i){ alert(i); })(3);
I mean , where are the wrappers () ?
source share