2

I see no situation where I need this

(function(param){
alert(param);
//do something
})("derp");

istead of this

alert("derp");
//do something

EDIT: ok, thanks everybody, i think i got it. so if you have this:

var param = "x";
var heya = "y";

(function(param){
    alert(param);
    //do something
    })(heya);

the global variable "param" will be ignored in the scope of the anonymous function?

melanke
  • 804
  • 1
  • 9
  • 25
  • On the matter of anonymous function - [setTimeout()](https://developer.mozilla.org/en/DOM/window.setTimeout) – Bakudan Jan 27 '12 at 14:22
  • 3
    possible duplicate of [How does the (function() {})() construct work and why do people use it?](http://stackoverflow.com/questions/1639180/how-does-the-function-construct-work-and-why-do-people-use-it) – Lucero Jan 27 '12 at 14:22
  • Per your edit, yes you are correct. param === heya in the scope of your [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) – John Strickler Jan 27 '12 at 14:37
  • @melanke, Yes, your global `param` will remain untouched by the inner `param` (and no-one can alter your private inner `param`). – Lucero Jan 27 '12 at 14:38

4 Answers4

3

How about this scenario?

Example #1 - Uses an Immediately Invoked Function Expression that closes over a single variable and returns another function. Resulting in a beautiful, encapulated function.

var tick = (function () {
   //Example of function expression + closure
   var tock = 0;

   return function() {
        return ++tock;
   }
}());

//It is impossible to alter `tock` other than using tick()
tick(); //1
tick(); //2
tick(); //3
tick(); //4

VERSUS

Example #2 - Uses a Function Declaration w/ a global variable

//Unnecssary global (unless wrapped in another function, such as jQuery's ready function)
var tock = 0;

function tick() {
    return ++tock;
}

tick(); //1
tock = 4; //tock is exposed... and can be manipulated
tick(); //5
tock = 6;
tick(); //7

It is a contrived example but still a real case scenario in situations where people may want to generate consecutive UNIQUE ID's with no possibility of collision.

John Strickler
  • 25,151
  • 4
  • 52
  • 68
1

Well, you don't - for this simple example.

In JavaScript, variables are scoped to functions; therefore, if you wrap it in a function, you avoid global namespace pollution.

Lucero
  • 59,176
  • 9
  • 122
  • 152
1

Well, in the example you've given, no, there probably isn't any reason why you would do this.

However, such a pattern is used typically to ensure that variables or functions, you require at a global level

  • Can be isolated from others potentially defined in other libraries
    • thus is an effective way to hide private variables
  • Can be protected against being tampered with by other libraries

Globals in Javascript are evil.

In particular, when working with jQuery I will frequently enclose the $(callback(){}) in a function like this, so that I can have global state for the jQuery code that I don't want inside the callback itself, usually because I have other code that isn't necessarily dependant on the jQuery ready initialisation:

function(){
  var something = 'something';
  $(function(){
    something = 'jQuery bound';
  });
}();
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
0

Aside from polluting the global namespace, memory usage is the other reason to do this - if you're pulling in a big data structure that only gets used once, wrapping that in a (function(){})() block means that you know it will be cleaned up once the function is finished executing.

Dan Monego
  • 9,637
  • 6
  • 37
  • 72