1, what is closure
Closures, the official explanation for closures, is that an expression (usually a function) that has many variables and an environment bound to them is also part of the expression.
Simply put, JavaScript allows the use of intrinsic functions---i.e. function definitions and function expressions in the function body of another function. Furthermore, these internal functions can access all the local variables, arguments, and other internal functions declared in the external function in which they are located. A closure is formed when one of these intrinsic functions is called outside the external function that contains them.
The characteristics of closure
1 function Nesting function
2 functions can refer to external parameters and variables inside
3 parameters and variables are not reclaimed by the garbage collection mechanism
After the normal function is finished, the local active object is destroyed and only the global scope is saved in memory. But the closures are different!
function fn () {
var a =;
function fn () {
//can access the A value
alert (a++) defined in FN;
}
fn ();
}
FN ();
fn ();//
function fn () {
var a =;
function fn () {
//can access the A value
alert (a++) defined in FN;
}
Return fn;//
}
var f = fn ();
f (); After execution, a is still in memory
f ();//
F = null;//A is recycled
The above is a small series to introduce JavaScript in the closure of the package, I hope to help you!