Home  >  Article  >  Web Front-end  >  N ways to write anonymous functions in js_javascript skills

N ways to write anonymous functions in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:20:031103browse

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:

Copy code The code is as follows:

(function() {
alert('water');
})();

Of course you can With parameters:
Copy code The code is as follows:

(function(o) {
alert(o);
})('water');

Want to use chain calls of anonymous functions? Very simple:
Copy code The code is as follows:

(function(o) {
alert(o);
return arguments.callee;
})('water')('down');

You know all the common anonymous functions, let’s see if they are uncommon of:
Copy code The code is as follows:

~(function(){
alert ('water');
})();//The writing is a bit cool~

Copy code The code is as follows:

void function(){
alert('water');
}();//It is said to be the most efficient~

Copy code The code is as follows:

function(){
alert('water');
}();

Copy code The code is as follows:

-function(){
alert('water');
}();

Copy code The code is as follows:

~function(){
alert('water');
}();

Copy code The code is as follows:

!function(){
alert('water');
}();

Copy code The code is as follows:

( 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~
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn