There are many methods for defining functions in js. the direct quantity of functions is one of them. The following article describes how anonymous functions are called and how to write them in n, if you are interested in js anonymous function calling and js anonymous function writing, you can learn about the various methods of defining functions in Javascript. the direct quantity of functions is one of them. For example, var fun = function () {}. if function is not assigned to fun, it is an anonymous function. Okay. let's see how anonymous functions are called.
Method 1: Call the function to obtain the returned value. Forced operator for function call execution
(function(x,y){alert(x+y);return x+y;}(3,4));
Method 2: Call the function to obtain the returned value. Force the function to execute directly and return a reference, and then call and execute the reference.
(function(x,y){alert(x+y);return x+y;})(3,4);
This method is also a popular call method for many libraries, such as jQuery and Mootools.
Method 3: Use void
void function(x) {x = x-1;alert(x);}(9);
Method 4: use the-/+ operator
-function(x,y){alert(x+y);return x+y;}(3,4);+function(x,y){alert(x+y);return x+y;}(3,4);--function(x,y){alert(x+y);return x+y;}(3,4);++function(x,y){alert(x+y);return x+y;}(3,4);
Method 5: Use a tilde (~)
~function(x, y) {alert(x+y);return x+y;}(3, 4);
Method 6: put anonymous function execution in brackets
[Function () {console. log (this) // The console output window in the browser} (this)]
Method 7: add typeof to the front of the anonymous function
Typeof function () {console. log (this) // The console output window in the browser} (this)
Method 8: add delete before the anonymous function
Delete function () {console. log (this) // The console output window in the browser} (this)
Method 9: add void before the anonymous function
Void function () {console. log (this) // The console output window in the browser} (this)
Method 10: use the new method to pass parameters.
new function(win){console.log(win) // window}(this)
Method 11: Use new without passing parameters
New function () {console. log (this) // Here this is not a window}
Method 12, comma operator
function(){console.log(this) // window}();
Method 13, bitwise XOR operator
^function(){console.log(this) // window}();
Method 14, comparison operator
function(){console.log(this) // window}();
Finally, let's take a look at the incorrect call method.
function(x,y){alert(x+y);return x+y;}(3,4);
N anonymous functions are written as follows:
How can an anonymous function be executed without a real name or pointer?
The writing of anonymous functions is very divergent ~
+ Indicates converting a function declaration to a function expression. Summary
The most common usage:
The code is as follows:
(function() { alert('water'); })();
You can also include the following parameters:
The code is as follows:
(function(o) { alert(o); })('water');
Do you want to use the chained call of anonymous functions? Simple:
The code is as follows:
(function(o) { console.log(o); return arguments.callee; })('water')('down');
Common anonymous functions are all known. See the following:
The code is as follows:
~ (Function () {alert ('water') ;}) (); // The writing method is cool ~
The code is as follows:
Void function () {alert ('water') ;}(); // It is said that the efficiency is the highest ~
The code is as follows:
+function(){ alert('water'); }();
The code is as follows:
-function(){ alert('water'); }();
The code is as follows:
~function(){ alert('water'); }();
The code is as follows:
!function(){ alert('water'); }();
The code is as follows:
(Function () {alert ('water') ;}(); // a bit forced to execute ~
I have shared with you the call and writing of anonymous functions in Javascript, hoping to help you.