当前位置:网站首页>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
边栏推荐
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- DDT + Excel for interface test
- Kingdee Cloud Star API calling practice
- Website_ Collection
- BUG_ me
- RTKLIB 2.4.3源码笔记
- An essay on the classical "tear down the wall in thinking"
- Production environment——
- Milvus 2.0 質量保障系統詳解
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
猜你喜欢
[PROJECT] small hat takeout (8)
Selenium IDE and XPath installation of chrome plug-in
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
How to build tiktok user trust and drive fan growth
Milvus 2.0 détails du système d'assurance de la qualité
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
loggie 源码分析 source file 模块主干分析
Pycham connects to the remote server and realizes remote debugging
Installing labellmg tutorial in Windows
随机推荐
Selenium IDE and XPath installation of chrome plug-in
Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
freeCodeCamp----prob_ Calculator exercise
First knowledge of go language
ByteVCharts可视化图表库,你想要的我都有
Nacos + aspnetcore + Ocelot actual combat code
Milvus 2.0 質量保障系統詳解
Getting started with JDBC
Pseudo Distributed installation spark
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Camtasia2022软件新增功能介绍
面试百分百问到的进程,你究竟了解多少
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
Node access to Alipay open platform sandbox to achieve payment function
PHP高效读大文件处理数据
About stream flow, write it down briefly------
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
Production environment——
批量制造测试数据的思路,附源码