Examples of calling and writing anonymous functions in Javascript (multiple) and javascript
There are multiple methods to define functions in Javascript, and 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.
Articles you may be interested in:
- N writing methods of anonymous functions in js
- Summary of multiple call methods for anonymous functions in Javascript
- Specific implementation of self-called anonymous functions in JS
- Example of calling js anonymous functions (diverse forms)
- Create and call methods of anonymous functions in js