Home > Web Front-end > JS Tutorial > body text

Specific implementation of JS self-calling anonymous functions_javascript skills

WBOY
Release: 2016-05-16 17:00:43
Original
1039 people have browsed it

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 );
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template