JS since the call anonymous function concrete implementation


In js, a function is often defined as a temporary namespace in which variables are defined without polluting the global namespace (preventing local variables from colliding with global variables).


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

It can be abbreviated as:


(function(){ //The mymodule() function is overwritten as an anonymous function expression
//Module code
}( )<span style="color:#ff0000;">)</span>; //Close the function definition and call it immediately

Or:


(function(){

}<span style="color:#ff0000;">)</span>();

This method of defining anonymous functions and calling them immediately (from within) has become so common that it’s starting to get a little confusing. The source code for jquery is written like this:


(function( window, undefined ) {

//All the code for jquery
})( window );