Error mode: Syntax error warning
function(){
// insert code here
}();
Mode 1: Function Literal (Function Literal)
Declare the function object first and then execute it.
(function(){
// insert code here
})();
Mode 2: Priority Expression (Prior Expression)
Since JavaScript executes expressions in order from the inside to the outside, brackets are used to force execution of a declared function.
(function(){
// insert code here
}());
Mode 3: Void Operator (Void Operator)
Use Void operator to execute a single operand.
void function(){
// insert code here
}();
Technically, these three code patterns are equivalent. But in actual applications, such as YUI, jQuery and other frameworks, mode 1 is more widely used.