2019.8.2 closure scope

 

Scope:

A 10 = var;
function Fun (A) {
// var A = 100;
A ++;
the console.log (A);
}
Fun (A); 101 //
the console.log (A); // 10
// scope available range of a variable: (scope)
//. 1, the global scope window: global variables
// everywhere, repeated use ==> global pollution
// 2, scope of function: local variables
// local variables (AO: Action Object): available only within the function, can not be used repeatedly
// function lifecycle:
// 1) before (early life stages) begin
// create an execution environment stack (array): temporarily hold execution environment functions being performed
// add to the main program the first default execution environment (), which creates a global scope object window

 

// 2) the definition of the function
// create function objects defined wrapper function
// declare a variable function, cited function object, scope attribute function object reference back, the scope when creating function
when // 3) call the function
/ / add a new element (execution environment) is recorded in the ECS call the new function
// create an active object, local variables used in this call (function parameters, using the data in the var keyword to declare a function in vivo)
the implementation of new environmental elements // ESC, the references in the active object
// parent properties for the active object references the parent scope objects scope of function points
// function: use priority active object in a local variable in the implementation process, local no, just look for the extension of a parent to the parent scope
// 4) after the function call
// execution environment stack execution environment of this function is the stack
// lead to active objects are released, resulting in the release with variable Bureau

 


// scope chain
// Right scope multistage continuous chain resulting in the formation of references
// master sequence using all variables: applied locally to the first, there is no extension of the scope chain to find the parent scope

 

Closure:

 

// 全局变量:可以反复使用,随处可用 -- 全局污染
// 局部变量:不可以重用 ,只在函数中使用
// 闭包:即重用变量,又保护变量不被污染的一种机制
// 取号
// 1、用外层函数包裹受保护的变量和操作变量的内层函数
function outer(){
var i = 1;//活动对象,函数作用域
i = i + 2;//3
// 2、外层函数将内层函数返回
return function getNum(){
console.log(i++);
}
}
// 3、使用者调用外层函数,获得内存函数的对象
var getNum = outer();
//getNum:function getNum(){console.log(i++)}
// 问题:outer()结束后,outer的活的对象释放?
getNum();//1
getNum();//2
getNum();//3

 

//......
i = 1;//window对象下的变量

 

getNum();//4
getNum();//5

 

// 缺点:比一般函数占用更多内存
// 简易的闭包流程图
// 1、先找到受保护的变量,判断外层函数执行后变量的结果
// 2、再找操作变量的内层函数
// 1)被外层函数return到外部
// 2)也可通过直接给全局变量赋值
// 题目:
var nAdd;
function outer1(){
var n = 999;
nAdd = function(){n++};
return function(){console.log(n)}
}

 

var getN = outer1();
// nAdd();
getN();//999

 

function fun(){
for(var i = 0,arr= [];i<3;i++){
arr[i] = function (){console.log(i)}
}
return arr;
}
var funs = fun();
funs[0]();//? 3
funs[1]();//? 3
funs[2]();//? 3

 

 

Guess you like

Origin www.cnblogs.com/awei313558147/p/11309536.html