I had this code :
function (i)
{
alert(i);
}(3);
And it wasn't working , So After 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 other 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); // <=== What about this ? there is no () wrapper... so how does it work ?
document.body.appendChild(link);
}
}
window.onload = addLinks;
I wanted to ask what is the role for the (i)
part ? Is it executing something ?
And if it Does why Its not in a pattern of :
(function(i){ alert(i); })(3);
I mean where is the wrappers of () ?