当前位置:网站首页>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
边栏推荐
- How to implement distributed locks with redis?
- Milvus 2.0 質量保障系統詳解
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- MySQL master-slave synchronization pit avoidance version tutorial
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- _ Mold_ Board_
- [registration] tf54: engineer growth map and excellent R & D organization building
- 面试百分百问到的进程,你究竟了解多少
- Decimal format decimal / datetime conversion processing
- freeCodeCamp----prob_ Calculator exercise
猜你喜欢

Get the column name list of the table quickly in Oracle

Milvus 2.0 质量保障系统详解

Installation and management procedures

Lock lock

Modify the test case name generated by DDT

New project of OMNeT learning

vscode如何比较两个文件的异同

无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择

Sub database and sub table & shardingsphere

Detailed explanation of Niuke - Gloves
随机推荐
Project framework of robot framework
freeCodeCamp----prob_ Calculator exercise
详解牛客----手套
AIOT产业技术全景结构-数字化架构设计(8)
Idea of batch manufacturing test data, with source code
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
feign报400处理
Use case execution of robot framework
Introduction to how to set up LAN
Rtklib 2.4.3 source code Notes
英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
◰GL-阴影贴图核心步骤
◰GL-着色器处理程序封装
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
How does flash cache data in memory?
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Installation and management procedures
SQL database
Handwritten event publish subscribe framework