当前位置:网站首页>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

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
边栏推荐
- Change the password after installing MySQL in Linux
- Paging SQL
- 扫码登录的原理你真的了解吗?
- DDT + Excel for interface test
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- On lambda powertools typescript
- AIOT产业技术全景结构-数字化架构设计(8)
- Read a blog, re understand closures and tidy up
- 网络安全之渗透靶场实战详解
猜你喜欢

英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)

网络安全之渗透靶场实战详解

Installing labellmg tutorial in Windows

Document operation II (5000 word summary)

Do you really understand the principle of code scanning login?

Path environment variable

Idea of batch manufacturing test data, with source code

Installation and management procedures

Modify the test case name generated by DDT

DDT + Excel for interface test
随机推荐
PostgreSQL column storage and row storage
MySQL master-slave replication
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
Mock test
Decimal format decimal / datetime conversion processing
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
网络安全之渗透靶场实战详解
手写事件发布订阅框架
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
Your brain expands and shrinks over time — these charts show how
织梦DEDECMS安全设置指南
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
Tencent resolves the address according to the IP address
Regular filtering of Intranet addresses and segments
Installing labellmg tutorial in Windows
Shell脚本——Shell编程规范及变量
Detailed explanation of UWA pipeline function | visual configuration automatic test
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
websocket