I don't know how to understand the concept of closures, I understand the closure is:
When a function A contains another function B,c calls function A, it is actually called function B, which is the closure (the inner function nested inside the function, the intrinsic function can access the local variables, arguments, and other intrinsic functions declared in the outer function, and the inner function is called by the external function ).
Let's look at an example:
function A () { //a contains the other functions B
var i=0;
Function B () {
Alert (++i)
};
Return b
};
var c=a (); when C calls function A, it actually calls function B.
alert (c); Function B () {Alert (++i)} is returned
Closures are actually the protection of internal variables,
But if these variables are not needed on the page, it can cause garbage.
will increase the memory.
C=null so I will be recycled.
What are the benefits of closures?
1) can exist its own private variables;
2) Avoid the influence of global variables;
3) Keep a variable stored in memory for a long time
Understanding JavaScript Closures