Closure
Concept of closures: the function A, a function B, function B can access function A defined variables or data, at this time the closure is formed (this sentence temporarily stringency)
- Closure mode: Mode closure function, object model closure
- Action closures: cache data, to extend the scope chain
- Closure of the advantages and disadvantages: the data cache
- Closures
Mode closure function: a function in a function
// function closure mode: there is a function in a function function F1 () { var NUM = 10 ; // declare function of function F2 () { the console.log (NUM); } // function calls F2 () ; } F1 ();
function f1 () {
were num = 10 ;
return function () {
console.log (num); 10 //
return num; } }
Was ff = f 1 (); were result = ff (); console.log (result); // 10
Closure object model: there is a target function
function f3 () { were num = 10 ; was obj = { Age: num }; console.log (obj.age); // 10 } f3 ();
function f2 () { were num = 100 ; Return { Age: num } } were obj = f2 (); console.log (obj.age);