开发者

What does this block of JS code mean? [duplicate]

This question already has answers here: Closed 11 years ago.

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?


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.


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.


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.


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..


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜