当前位置:网站首页>43.js -- scope chain
43.js -- scope chain
2022-07-28 02:58:00 【Like to drink pearl milk tea】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
1. Scope chain concept
stay JavaScript in , Functions are also objects , actually ,JavaScript It's all about objects . Function objects are just like other objects , Has properties that can be accessed by code and a series of properties JavaScript Internal properties accessed by the engine .
[[scope]] A collection of runtime context objects stored in , This set is linked in a chain , We call this chain connection Scope chain of function , It determines which data can be accessed by functions .
The purpose of the scope chain is Ensure orderly access to all variables and functions that the execution environment has access to .
2. Execution context
When the function executes , An internal object called the execution context is created . An execution time context defines the environment in which a function executes , Each time a function executes, the corresponding execution context is unique , So calling a function multiple times can result in multiple execution contexts being created , When the function is finished executing , The execution context it generates is " The destruction ".
<pre> analysis 1: Execution context refers to the context generated immediately before the function is executed AO object </pre>
<pre> analysis 2: The function execution environment refers to those obtained by the variable promotion function AO Object properties </pre>
<pre> analysis 3:
function test() { }Function called many times , Produce different AO object :
test(); ---->AO{}
test(); ---->AO{}
After the function is executed, the corresponding AO Destruction of objects .
</pre>
[[scope]] It refers to what we call the scope , A collection in which the runtime context is stored .
js Object has two kinds of members
One is the above member (js Members whose syntax can be accessed directly )
One is the following members ( Members accessed by the underlying syntax )
[[scopes]] Enclosed member names Is the following member
function fn () { } console.dir(fn)This " object " What is saved internally is the scope of the function
The function is defining / When making a statement And then there is [[scopes]] The upper layer is preserved inside AO object
Function call will generate AO object AO Save in scopes Inside the object
for example :
function fn (b) { var a=20 function fm () { } } fn(100) fn(200)
When the function is generated, there will be an attribute [[scopes]] Scope " Array "( Engine only )
Generated when the function is called AO object Will be able to AO The object is placed in scopes
Each call will be placed in scopes front ( Top )
Each function scopes There is one in the array AO object It is the upper layer of this function AO
Case study 1:
function fn() { var a = 30 function fm() { var b = 20 console.log(a) } fm() fm() } fn() fn()Running results :
Case study 2:
function fn(a) { function fm() { var b = 20 console.log(a) } fm() fm() } fn(100) fn(200)Running results :
Program :
function a() { var aaa = 123; function b() { var bbb = 234; console.log(aaa); } return b; } var glob = 100; var demo = a(); demo();Running results :
Execution process :
Go:{a function ,glob:100,demo:a.[[scopes In the object ,b function ]]}
a.[[scopes]]==>[AO:{aaa:123,b function ,},Go]
b function .[[scopes]]==>[,a.[[scopes]]]<====>[AO(b):{bbb:234, Print aaa},AO(a):{aaa:123,b function ,},Go]
边栏推荐
- 写英文IEEE论文的技巧
- JS 事件对象2 e.charcode字符码 e.keyCode键码 盒子上下左右移动
- 数据中台夯实数据基础
- Explanation of CNN circular training | pytorch series (XXII)
- Confusion matrix in CNN | pytorch series (XXIII)
- 数据中台建设(三):数据中台架构介绍
- JS event object offsetx/y clientx y pagex y
- Pytest the best testing framework
- 修改MySQL密码的四种方法(适合初学者)
- Cesium3Dtilesets 使用customShader的解读以及泛光效果示例
猜你喜欢

别人发你的jar包你如何使用(如何使用别人发您的jar包)

初识C语言 -- 结构体,分支和循环语句

Opengauss Developer Day 2022 sincerely invites you to visit the "database kernel SQL Engine sub forum" of Yunhe enmo

First knowledge of C language -- operators and keywords, define, pointer

Pytest the best testing framework

Constant power wireless charging based on stm32

【信号处理】基于高阶统计量特征的通信系统中微弱信号检测附matlab代码

修改MySQL密码的四种方法(适合初学者)

How do gateways and chirpstacks in lorawan communicate? UDP? GRPC? MQTT?

RTSP/Onvif协议EasyNVR视频平台一键升级方案的开发设计逻辑
随机推荐
初识C语言 -- 操作符和关键字,#define,指针
Is it you who are not suitable for learning programming?
【OpenGL】GLES20.glClear
【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
数据中台夯实数据基础
Newline required at end of file but not found.
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.
[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass
[software testing] - unittest framework for automated testing
CNN training cycle reconstruction - hyperparametric test | pytorch series (XXVIII)
智能工业设计软件公司天洑C轮数亿元融资
新基建助力智能化道路交通领域的转型发展
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
Newline required at end of file but not found.
windbg
Some shortest path problems solved by hierarchical graph
MySQL索引学习
[TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal
JS 事件对象 offsetX/Y clientX Y PageX Y


