There are many ways to define functions in JavaScript, and the direct amount of the function is one of them. such as var fun = function () {}, where function does not assign value to fun then it is an anonymous function. OK, let's see how anonymous functions are invoked.
Mode 1, call the function, get the return value. The force operator makes the function call execute
(function (x,y) {
alert (x+y);
return x+y;
Mode 2, call the function, get the return value. Force the function to perform a direct amount of execution and return a reference, and then invoke the execution
(function (x,y) {
alert (x+y);
return x+y;
This is also the way many libraries love to call, such as Jquery,mootools.
Mode 3, using void
void function (x) {
x = x-1;
alert (x);
} (9);
Mode 4, using 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;
Mode 5, using the tilde (~)
~function (x, y) {
alert (x+y);
return x+y;
Mode 6, anonymous function execution placed in brackets
[function () {
Console.log (this)/Browser console Output Window
Mode 7, anonymous function before adding typeof
typeof function () {
Console.log (this)/Browser console Output Window
Mode 8, anonymous function before adding delete
Delete function () {
Console.log (this)/Browser console Output Window
Mode 9, anonymous function before adding void
void function () {
Console.log (this)/Browser console Output Window
Mode 10, using the new method, the argument
New function (Win) {
Console.log (Win)//window
Mode 11, use new, do not pass the argument
New function () {
Console.log (this)//this is not window.
Mode 12, comma operator
function () {
Console.log (this)//window
Mode 13, bitwise XOR OR operator
^function () {
Console.log (this)//window
Mode 14, comparison operator
function () {
Console.log (this)//window
Finally look at the wrong way to call
function (x,y) {
alert (x+y);
return x+y;
The n types of anonymous functions are written as follows
Anonymous functions have no actual names, no pointers, how do they execute?
About anonymous function writing, very divergent ~
The + number is to convert the function declaration to a function expression. Sum up
The most common usage:
The code is as follows:
(function () {
alert (' Water ');
Of course, you can also take parameters:
The code is as follows:
(function (o) {
alert (o);
Want to use a chained call to an anonymous function? Very simple:
The code is as follows:
(function (o) {
console.log (o);
return Arguments.callee;
Common anonymous functions are known to look at uncommon:
The code is as follows:
~ (function () {
alert (' Water ');
The code is as follows:
void 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 ');
The code is as follows:
(function () {
alert (' water ');
} ())//a bit of a compulsive taste ~
The above to share the JavaScript in the anonymous function of the call and write, I hope to help.