Closures: Closures are functions that have access to variables in another function scope, and the common way to create closures is to create another function inside one function
(from the definition, all functions in JS are closures)
function A () { var i=0; function B () { alert (+ +i) ; } return b;} var c=A (); C ();
Features of the above code:
1. function b is nested inside function A;
2, function a returns function B.
When function A's intrinsic function B is referenced by a variable outside of function A, a closure is created.
A closure is a function or block of code that does not reclaim the resources used in a, because the execution of A's intrinsic function relies on the variables in a
PS: A variable object of the global environment always exists, like a local environment variable in a function, only exists during function execution
A scope chain is essentially a list of pointers to variable objects that reference but do not actually contain variable objects.
PS: Because the closure carries the scope of the function that contains it, it consumes more memory than the other functions. Transitions using closures may result in excessive memory consumption
So, in essence, a closure is a bridge that connects the inside of the function to the outside of the function.
Use of closures:
A. Read the variables inside the function.
B. Keep the values of these variables in memory at all times.
Note points for using closures
1) Because the closure will make the variables in the function are stored in memory, memory consumption is very large, so can not abuse closures,
Otherwise, the performance of the Web page may cause a memory leak in IE. The workaround is to remove all unused local variables before exiting the function.
2) The closure changes the value of the inner variable of the parent function outside the parent function. So, if you use the parent function as an object,
Consider the closure as its common method, and take the internal variable as its private property (private value).
Be careful not to arbitrarily change the value of the internal variables of the parent function.
Problems caused by closures:
function A () { var result = new Array (); for (var i=0; i <; I++) {Result[i] = function (num) { return function () { return Num }; } (i); If you do not pass the I-value run code here, the value obtained is ten} return result;}
var name = "the window"; var object ="My object"function() { returnfunction () { returnthis//"The Window" (in non-strict mode)
Object.getnamefunc () returns the parentheses after a function and runs the function as if it were running in the global environment.
In the global environment this is the default point to the Window object
So the this.name inside the function is the global variable name.
Workaround:
We know that one of the functions of closures is to read the variables inside the function.
If you want to get the original object you can save the original this in another custom variable and then pass the variable into the returned function.
This will keep the variable in memory and will not be destroyed.
The closure in JS