An anonymous function has no actual name and no pointer. How to execute it?
In fact, you can understand the meaning of the parentheses. Parentheses have a return value, which is the return value of the function or expression within the parentheses. Therefore, the return value of the function within the parentheses is equal to the return value of the parentheses. It is not difficult to understand that (function(){})() can be The function without a name was executed...
Regarding the writing method of anonymous functions, it is very divergent ~
The most common usage:
(function() {
alert('water');
})();
Of course you can With parameters:
(function(o) {
alert(o);
})('water');
Want to use chain calls of anonymous functions? Very simple:
(function(o) {
alert(o);
return arguments.callee;
})('water')('down');
You know all the common anonymous functions, let’s see if they are uncommon of:
~(function(){
alert ('water');
})();//The writing is a bit cool~
void function(){
alert('water');
}();//It is said to be the most efficient~
function(){
alert('water');
}();
-function(){
alert('water');
}();
~function(){
alert('water');
}();
!function(){
alert('water');
}();
( function(){
alert('water');
}());//It feels a bit mandatory~
So many writing methods are sold cheaply~ Haha, actually some People consider the efficiency of writing methods. If you can, give me some data. I feel that these writing methods are efficient, but they should be negligible (maybe wrong). I will choose one at will~