js高级_执行上下文(JS advanced_ Execution context)

1、代码分类(位置)

全局代码函数(局部)代码

2、全局执行上下文对象

①在执行全局代码前将window确定为全局执行上下文对象(全局执行上下文对象每次执行js代码只存着一个)

②对全局数据进行预处理*var定义的全局变量 = =>undefind,添加为window的属性。*function声明的全局函数 = =>赋值(fun),添加为window的方法。先执行变量提升再执行函数提升。*this = =>赋值(window)。

③开始执行全局代码

全局执行上下文对象
console.log(a1);//相当于从全局上下文window里找a1,也就是window.a1
var a1=3;

a2();//相当于从全局上下文window里找a2,也就是window.a2();

function a2(){
    	console.log(555);
}

3、函数执行上下文对象

①在调用函数,准备执行函数体之前,创建对应的函数执行上下文对象(该对象是临时的,虚拟的,放在栈里的,在函数被调用时才创建,是栈单独给它划了一小块封闭的空间,这个空间就称为对象,只不过这个对象是存着栈的,而且里面存的是函数的形参,局部变量,参数列表等,外部是无法访问这个区域的数据的,而且函数调用完毕,该区域自动清除消失)。函数执行上下文对象只有在函数被调用时才会被创建。

②对局部数据进行预处理*形参变量= =>赋值(实参)= =>添加为函数执行上下文对象*arguments= =>赋值(参数列表),添加为函数执行上下文对象的属性。*var定义的局部变量= =>undefind,添加为函数执行上下文对象的属性。*function声明的函数= =>赋值(fun),添加为函数执行上下文对象的方法。*this= =>赋值(调用函数的对象)

③开始执行函数体代码

 function fn(a1){
    	
    	//函数这里能访问的有
    	console.log(a1);
    	console.log(a2);
    	a3();
    	console.log(this);
    	console.log(arguments);//伪数组
    	
    	var a2=3;
    	
    	function a3(){
    		console.log(555)
    	}
    	
    }
    
    
    fn(2,3)
————————

1. Code classification (location)

Global code function (local code)

2. Global execution context object

① Before executing the global code, determine the window as the global execution context object (the global execution context object only stores one JS code each time it executes)

② Preprocess global data * global variable defined by VAR = = & gt; Undefind, added as the attribute of window* Global function declared by function = = & gt; Assign a value (fun) and add it as the method of window. Execute variable promotion first and then function promotion* this = => Assignment (window).

③ Start executing global code

全局执行上下文对象
console.log(a1);//相当于从全局上下文window里找a1,也就是window.a1
var a1=3;

a2();//相当于从全局上下文window里找a2,也就是window.a2();

function a2(){
    	console.log(555);
}

3. Function execution context object

① Before calling the function and preparing to execute the function body, Create the corresponding function execution context object (the object is temporary, virtual and placed on the stack. It is created only when the function is called. The stack delimits a small closed space for it. This space is called the object, but the object is stored in the stack, and the formal parameters, local variables, parameter list, etc. of the function are stored inside. The data in this area cannot be accessed externally, and the area will be cleared and disappear automatically after the function is called.). The function execution context object is created only when the function is called.

② Preprocess local data * formal parameter variable = = & gt; Assignment (argument) = & gt; Add as function execution context object * arguments = = & gt; Assignment (parameter list), added as the property of the function execution context object* Local variable defined by VAR = = & gt; Undefind, which is added as the property of the function execution context object* Function declared function = = & gt; Assign a value (fun) to add a method as the execution context object of the function* this= => Assignment (the object that calls the function)

③ Start executing function body code

 function fn(a1){
    	
    	//函数这里能访问的有
    	console.log(a1);
    	console.log(a2);
    	a3();
    	console.log(this);
    	console.log(arguments);//伪数组
    	
    	var a2=3;
    	
    	function a3(){
    		console.log(555)
    	}
    	
    }
    
    
    fn(2,3)