Do three minutes to understand simple closures

Summary:

Since the function can not access the internal function of external variables local (private property). Because the variable inside the function, after the function completes, it will be released.

Can not directly access it indirectly to achieve, look at the example

<Script> 
var = 200 is A;
function the Fn () {
var A = 100;
return function () {
the console.log (A);
}
}
var the Fn = F1 ();
; F1 ()
</ Script>

internal function , the definition of a subroutine, subroutine can access the parent function private variables. Functions then be accessed and then the subroutine returns by return.
f1 () can be executed after the print functions Fn private variables a = 200. 

Simple closures it is colloquially:

   Closures function is the ability to read other function of the internal variables , so that the function is not garbage collection mechanism, so that a variable long-term presence in memory of them, if excessive use of closures, easily lead to memory leaks.

Benefit is the ability to read other private variables inside a function, to avoid problems due to the scoped variables defined in the global scope inside, causing pollution of global variables



expand:

    Parent scope of a function is the function defined scope of time, rather than in the implementation of the scope of time.

Guess you like

Origin www.cnblogs.com/ll15888/p/11163191.html