5

Possible Duplicate:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

I came across a JS file that can be summarized to the code below:

   (function(window){
              // some codes here
                    })(window);

I wonder what this block of code means? Has the window special meanings, or it is just a parameter? What are the differences between the two "window" we see in the brackets?

Since this function does not have a name, I assume it is an anonymous function, so is it only invoked once? When is it invoked?

Sebastian Hofmann
  • 1,440
  • 6
  • 15
  • 21
Codier
  • 2,126
  • 3
  • 22
  • 33

5 Answers5

4

This is called a immediately-invoked anonymous function. (IIAF for short.)

In this case, you are defining a function that takes in a parameter called "window" that overrides the global window object inside of that scope.

The kicker here is that right after defining the function, you're immediately invoking it, passing in the global window object, so it is as if you used the global reference within the function closure!

Most of the time, the purpose of this is to avoid polluting the global namespace by way of wrapping all potential variables within an anonymous scope.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    It's not "self-executing" ! It's "Immediately executed". No part of the function executes itself. – Raynos May 16 '11 at 23:23
  • "self-invoked" would be more accurate. – Vivin Paliath May 16 '11 at 23:24
  • 1
    Mostly correct but not quite. The formal parameter `window` creates a local variable within the iife that is passed a reference to the global `window`. It doesn't "override" the global window, it references it. The local parameter is higher on the scope chain so it will be used instead of the global identifier, which is more commonly called "shaddowing". But they reference the same thing (whatever it is). – RobG May 16 '11 at 23:33
1

It's a closure. The code in question is a an anonymous function, which will execute with the "window" parameter (end of your snippet). It won't pollute the global namespace.

Roman
  • 13,100
  • 2
  • 47
  • 63
1

As far your questions regarding window, the window in parentheses at the bottom is a reference to the global window object. The first window is just a name for a parameter. But in this instance it refers to the global window object since you're using an anonymous self-invoked function. You could call it monkeys and it wouldn't make a difference (of course, you'd have to use monkeys within the body of the anonymous function then, to refer to the parameter). So now, you now have a reference to the global window object within your function.

Yes, the function is invoked once and it is invoked as soon as it is defined. This is because it is a self-invoked anonymous function.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • That is not the module pattern. An immediately invoked function expression (iife) can be used with Richard Cornford's module pattern, but they are different things. – RobG May 16 '11 at 23:30
  • @RobG you're right. That was a knee-jerk response. I'll fix it. – Vivin Paliath May 16 '11 at 23:41
0

The first window is formal parameter, while the second one is actual parameter that actually invokes the function. This type of function called self-invoking functions. The benefit of it is that wrapping functions this way doesn't clutter global scope..

DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72
0

It is an immediately invoked function expression. the grouping operator () around a function expression (essentially a function declaration without a name) means that the enclosed function is evaluated and a function object returned. A function followed by a formal parameter list (another set of ()) causes the function to be called, so:

(function() {
  alert('hey');
})();

creates an anonymous function that is called immediately and run once. It doesn't create any global variables and leaves no trace of its existence.

Passing the indentifier window to the function means that it is passes whatever it references. The presumption here (I suppose) is that it will reference a global window object that, in browsers, is the global object. However, in an environment that doesn't have a global window object, it may well be undefined. It is a pointless exercise in my view.

If you are concerned about getting a refernce to the global object, then pass this from the global context:

(function(global) {
  // global refernces the global object
})(this);
RobG
  • 142,382
  • 31
  • 172
  • 209