One. anonymous function 1. The definition of a function can be divided into three types
1) Function Declaration method
functionDouble(x) {
return2*x;
}
2) function constructor, the argument list and function body as strings, inconvenient, not recommended to use
varDouble=NewFunction (' x ', ' return 2*x; ');
3) function Expression style
varDouble= function (x) {
return2*x;
}
In this form, the right side of the equals sign is an anonymous function, and after the function is created, the function is assigned to the variable double.
2. Anonymous functions
1) The first way
var double= function (x) {
return2*x;
}
The right side of the equals sign is an anonymous function. Note that anonymous functions cannot be directly independent of the House code, as in the following code
Functino (x) {
return2*x;
}//syntaxerror:unexpected Token (
2) The second way
(function(x, Y) {
Console.log (X+y);
}) (2,3);
Creates an anonymous function (within the first parenthesis), and the second parenthesis is used to invoke the anonymous function and pass in the parameter.
Two. Closures (Closure)
Closure meaning: The outer function contains an inner layer function, and the inner function can access all the variables of the outer function, even if the outer function is finished. (JavaScript scope chain)
Off-topic: The above explanation for closures does not exactly match the JavaScript closure series I. As explained above, closure is only sufficient: the function inner nested inside the function outer. Other articles for closure interpretation, closure requires two conditions: 1) function inner nested inside function outer, 2) function outer return function inner. In this, I have been messy, the road warrior who can give a verdict?
Example 1: The function outer is executed instantaneously (about 0.00001 milliseconds), a variable str is created in the function outer body, and the STR variable is not released after outer execution, because the anonymous function in settimeout has a reference to the variable str. Until 2 seconds after the anonymous function is executed, STR is released.
functionOuter () {
varstr = "Closure";
SetTimeout (function() {
Console.log (str);
}, 2000);
}
Outer ();//Closure
Example 2: Is this case a closure?
functionOuter () {
vari = 22;
(functionInner () {
Console.log (i);
})();
}
Outer ();// A
Example 3: Simplifying code
functionFortimeout (x, y) {
Console.log (x + y);
}
functionDelay (x, Y, time) {
SetTimeout (' fortimeout (' + x + ', ' + y + ') ', time);
}
//after simplification
functionDelay (x, Y, time) {
SetTimeout (
function() {
Fortimeout (x, y);
}
, time);
}
Delay (3, 4, 2000);//7
Three. Anonymous functions and closures
The biggest use of anonymous functions is to create closures, which can also be used to build namespaces and reduce the use of global variables.
Example1: Addevent and Removeevent in anonymous functions are local variables, but can be used by global variables oevent, which greatly reduces the use of global variables and enhances the security of Web pages.
varOevent = {};
(function() {
varAddevent =function() {};
functionRemoveevent () {}
Oevent.addevent = addevent;
Oevent.removeevent = removeevent;
})();
JavaScript Closure Series Two---Closure of anonymous functions and functions