开发者

What actually does this javascript snippet mean?

From HTML5 Mobile Boilerplate's helper.js:

(function(document){
    //all stuff here
})(document);

What does this snippet do or when does it开发者_Go百科 run?


This is a closure, it defines a method which takes an argument document and immediately calls it with document as the parameter.

It runs as soon as it's finished evaluating - so basically straight away.


It creates a temporary, anonymous function and calls it with an argument called document. Presumably it has some local variables that it is hiding from the enclosing scope.


This is a javascript function that executes immediately when the browser encounters it while parsing the page. The function takes one parameter, which is the window.document property (as passed in at the bottom of the function.


If you say:

(function(var1){/*stuff*/})(var2)

That immediately calls the function and passes var2 to the function. Note that the function is anonymous and cannot be called directly. You can read about anonymous functions in general and anonymous functions in Javascript here:

http://en.wikipedia.org/wiki/Anonymous_function#JavaScript

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜