Home  >  Article  >  Web Front-end  >  Specific implementation of JS self-calling anonymous functions_javascript skills

Specific implementation of JS self-calling anonymous functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:00:431007browse

In js, a function is often defined as a temporary namespace. Variables defined in this namespace will not pollute the global namespace (to prevent conflicts between local variables and global variables).

Copy code The code is as follows:

function mymodule(){
//module code
}
mymodule();

can be abbreviated as:
Copy code The code is as follows:

(function(){ //mymodule() function is rewritten as an anonymous function expression
//module code
}( )); //End the function definition and call it immediately

or:
Copy the code The code is as follows:

(function(){

}) ();

This way of defining an anonymous function and calling it immediately (self-calling the anonymous function) has become very common, and it is starting to make people a little confused. The source code of jquery is like this Written:
Copy code The code is as follows:

(function( window, undefined ) {

//All code of jquery
})( window );
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn