An Analysis of javascript anonymous functions. If you are studying javascript, you can refer to the anonymous function, that is, a function without a name. For example:
The Code is as follows:
Function (){
Alert ('a function ');
}
However, the above Code will report an error. Firebug prompt: function statement requires a name, that is, the function must have a name.
The strange thing is that if I use a pair of () to package the function with no name, no error will be reported. For example:
The Code is as follows:
(Function (){
Alert ('a function ');
})
(Note the () of the function package ()!). Although no error will be reported in this way, who knows if this function is declared successful? Is it because there is no declaration? Let's test: Let the function execute it once:
The Code is as follows:
(Function (){
Alert ('a function ');
}())
As you can see, the function is executed, indicating that the function exists.
Similarly, if you remove the wrapped function () at this time, the previous error will still be reported and the function cannot be executed...
The Code is as follows:
Function (){
Alert ('a function ');
}()
This is so important to the () function of the package? Which expert explains the principle?