Closure has two features:
1. As a reference to a function variable-when a function is returned, it is activated.
2. A closure is a stack that does not release resources when a function returns.
Example 1. <SCRIPT type = "text/JavaScript">
Function sayhello2 (name ){
VaR text = 'hello' + name; // local variable
VaR sayalert = function () {alert (text );}
Return sayalert;
}
VaR Sy = sayhello2 ('Never-online ');
Sy ();
</SCRIPT>
As a javascript programmer, you should understand that the above Code is a reference to a function. If you still do not understand or do not know, please first understand some basic knowledge. I will not describe it here.
Why is the above Code a closure?
Because the sayhello2 function contains an embedded anonymous Function
Sayalert = function () {alert (text );}
In JavaScript. If you create an embedded function (as shown in the preceding example), a closure is created.
In C
Or in other mainstream languages, when a function returns, all local variables will be inaccessible because their stacks have been destroyed. But in Javascript, if you declare an embedded letter
Local variables are still accessible after the function returns. For example, the variable sy in the above example references the anonymous function () {alert (text) in the embedded function );
}, You can change the preceding example to the following: <SCRIPT type = "text/JavaScript">
Function sayhello2 (name ){
VaR text = 'hello' + name; // local variable
VaR sayalert = function () {alert (text );}
Return sayalert;
}
VaR Sy = sayhello2 ('Never-online ');
Alert (SY. tostring ());
</SCRIPT>
This is also consistent with the second feature of the closure.
Example 2. <SCRIPT type = "text/JavaScript">
Function say667 (){
// Local variable that ends up within Closure
VaR num = 666;
VaR sayalert = function () {alert (Num );}
Num ++;
Return sayalert;
}
VaR Sy = say667 ();
Sy ();
Alert (SY. tostring ());
</SCRIPT>
In the code above, the num in the anonymous Variable Function () {alert (Num);} is not copied, but continues to reference the value of the local variable defined by the external function-num, until the external function say667 () is returned.
Example 3. <SCRIPT type = "text/JavaScript">
Function setupsomeglobals (){
// Local variable that ends up within Closure
VaR num = 666;
// Store some references to functions as global variables
Required tnumber = function () {alert (Num );}
Gincreasenumber = function () {num ++ ;}
Gsetnumber = function (x) {num = x ;}
}
</SCRIPT>
<Button onclick = "setupsomeglobals ()"> Generate-setupsomeglobals () </button>
<Button onclick = "effectnumber ()"> output value-effectnumber () </button>
<Button onclick = "gincreasenumber ()"> Add-gincreasenumber () </button>
<Button onclick = "gsetnumber (5)"> value 5-gsetnumber (5) </button>
In the above example, the extension tnumber, gincreasenumber, and gsetnumber are all references to the same closure, setupsomeglobals (), because they declare that they all use the same global call -- setupsomeglobals ().
You can use the "generate", "add", "assign value", and "output value" buttons to view the output results. If you click "generate", a new closure is created. The three functions, including tnumber (), gincreasenumber (), and gsetnumber (5), are overwritten.
If you understand the above Code, see the following example:
Example 4. <SCRIPT type = "text/JavaScript">
Function buildlist (list ){
VaR result = [];
For (VAR I = 0; I <list. length; I ++ ){
VaR item = 'item' + list [I];
Result. Push (function () {alert (item + ''+ list [I])});
}
Return result;
}
Function testlist (){
VaR fnlist = buildlist ([1, 2, 3]);
// Using J only to help prevent confusion-cocould use I
For (var j = 0; j <fnlist. length; j ++ ){
Fnlist [J] ();
}
}
Testlist ();
</SCRIPT>
Running result:
Item 3 is undefined
Item 3 is undefined
Item 3 is undefined
Code result. Push (function () {alert (item + ''+ list [I])}),
Add reference of three anonymous functions to the result array. This code can also be written
VaR P = function () {alert (item + ''+ list [I])};
Result. Push (P );
Why is "item 3 is undefined" output three times"
The preceding example say667 () has already been explained.
Bytes
Name function () {alert (item + ''+
List [I]} In list [I] is not copied, but a reference to the parameter list. Until the function buildlist () is returned, that is, the last
Reference. That is, after traversing the list (Note: The maximum subscript of the list should be 2), it becomes 3 after I ++, which is why the item
3, and list [3] itself is not initialized, which is naturally undefined.
Example 5. <SCRIPT type = "text/JavaScript">
Function newclosure (somenum, someref ){
// Local variables that end up within Closure
VaR num = somenum;
VaR anarray = [1, 2, 3];
VaR ref = someref;
Return function (x ){
Num + = X;
Anarray. Push (Num );
Alert ('num: '+ num +
'Nanarray' + anarray. tostring () +
'Nref. somevar '+ ref. somevar );
}
}
VaR closure1 = newclosure (40, {somevar: 'Never-online '})
VaR closure2 = newclosure (99, {somevar: 'bluedestiny '})
Closure1 (4)
Closure2 (3)
</SCRIPT>
The last example shows how to declare two different closures.