当前位置:网站首页>Scope and scope chain in JS

Scope and scope chain in JS

2022-04-23 16:59:00 Endless cake

Scope and scope chain

First, let's look at an example :

			let x = 1;
			function A(y){
				let x = 2;
				function B(z){
					console.log(x+y+z)
				}
				return B
			}
			let C = A(2);
			C(3);

Let's start with ,js An order in which the underlying code is executed :

ECstack = [		// Execution stack 

	EC(G)={	// Create a global execution context 
		VO(G):{	// Global variable object 
		...			// Contains the original properties of the global object 
		x=1;
		A=function(y){...}		// The scope of the function is determined when it is created   ( a key )
		A[[scope]] = VO(G);
}
}
]

notes : No matter which execution context we are in , In addition to creating variables or heaps , At the same time, give the current function , Declare who its scope is .
Look at the example above , function A Created globally , that A The scope of is the global execution context . When A When it comes to execution , A new execution context will be generated ( Each function execution generates a private execution context ).
here , stay A Execution context , I created another B function . that B A private execution context will also be generated , meanwhile B The scope of will also be created , that B Is in A Created by the execution context of , that B The scope of the function is , Now this A Execution context .

Function in execution context , In addition to getting arguments( Argument object ), Generate out of scope , One more thing was done , Generate this Point to
Function execution , There is an executive body , The first thing I came in was to declare this Point to ,this Point back and say .

besides , It also initializes its linked list , That is what we often call scope chain . Search rules for scope chain , It's the bold text above .

So to summarize :
Function execution mechanism :
When you create a function :
(1) Create a heap ( Store the code string and the corresponding key value pair )
(2) The current scope is initialized [[scope]]
Scope [[scope]] Refers to the context EC Variable object in VO( The variable object )/AO( Variable object in function )
Strictly speaking . Scope [[scope]] It's different from asking questions up and down , Scope refers to the specific storage place of variables in the current context , Context is a variable that can be stored , It can compress the code and put it on the execution stack for execution . however , Programming , We think there's no difference between them , In fact, there will be no mistakes .
Function execution time
Create a new execution context EC, And compressed to the stack (EC stack) Internal execution .
initialization this Point to
Initialize scope chain
establish AO Variable objects store variables
The variable will change again arguments( It is the argument object of the function runtime ) =》 Shape parameter --》 If there is a variable increase , Raise the variable to , If there is no variable Promotion , Will execute the code .

So let's see , The above question is the formation of closures , And how to find the scope chain
 Insert picture description here
heavy :
When you create a function , What forms is the scope , And who the scope is , Function execution time , You must initialize its scope chain first , Code execution , Encountered a variable , First, let's see if it's a private variable , If it is , Using one's own , If not , Along the scope chain , Look up one level at a time , All the way to the global location , If you can't find it, you will report it wrong . This is the closure , A search process of scope chain .

版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554520051.html