Anonymous functions can effectively control the scope of variables and construct Closure to prevent global variables from being contaminated. Compile anonymous functions in JavaScript using the following methods: Error Mode: syntax error warning
The Code is as follows:
Function (){
// Insert code here
}();
Mode 1: Function Literal)
Declare the function object and then execute it.
The Code is as follows:
(Function (){
// Insert code here
})();
Mode 2: Prior Expression)
Since JavaScript executes expressions in the order from inside to outside, brackets are used to force the execution of declared functions.
The Code is as follows:
(Function (){
// Insert code here
}());
Mode 3: Void Operator)
Use the Void operator to execute a separate operand.
The Code is as follows:
Void function (){
// Insert code here
}();
Technically speaking, these three code modes are equivalent. However, in practical applications, such as YUI and jQuery frameworks, Pattern 1 is more widely used.