JS often defines a function for temporary naming space. The variables defined in this naming space will not pollute the global naming space (prevent local variables from conflicting with global variables).
Copy code code as follows:
function mymodule () {
// Module code
}
mymodule ();
Can be briefed as:
Copy code code as follows:
(Function () {// mymodule () function rewriting as anonymous function expression
// Module code
} () <span style = "color:#ff0000;">) </span>; // End the function definition and call it immediately
or:
Copy code code as follows:
(Function () {)
} <span style = "color:#ff0000;">) </span> ();
This definition of anonymous functions and immediately call (self -derived to call anonymous functions) is very common, and it is beginning to make people feel a little confused. This is how jQuery's source code is written:
Copy code code as follows:
(Function (Window, UNDEFINED) {
// All the code of jquery
}) (Window);