当前位置:网站首页>Interpreting closures from structured context
Interpreting closures from structured context
2022-04-22 12:50:00 【Front end sweeper】
There are two common statements about closures , The first is , A closure is a combination of a function and the Lexical Environment in which it is declared . This statement comes from MDN- Closure .
Another way of saying it , A closure is a function that has access to variables in the scope of another function .
Closures do have access to the functions of another function's scope variables , But closures are not functions .
Let's first understand the Lexical Environment
Lexical environment
We can see ES5 Chapter 10 of the code ( Executable code and execution context ) What does the lexical environment say in the second section of .
A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code.
Lexical environment is a canonical type (specification type), It defines the identifier and ECMAScript The relationship between specific variables and functions in code .
The problem is coming. , Type of specification (specification type) What is it again? ?specification type yes Type A kind of . from ES5 You can see in the specification Type It is divided into language types and specification types Two categories: .
language types It's the language type , The type we're familiar with , That is to use ECMAScript The types of data that programmers can manipulate , Include Undefined, Null, Number, String, Boolean and Object.
And the specification type (specification type) Is a more abstract meta value (meta-values), Used to describe in an algorithm ECMAScript The language structure and the specific semantics of language types .
We know , Executing the function creates a new lexical environment .
We also agree with ,with Statement will “ extend ” Scope ( It's actually called NewObjectEnvironment, Created a new lexical environment , The environment record of lexical environment is an object environment record ).
The above are easy to understand . that catch What does clause do to the Lexical Environment ? although try-catch I usually use it a lot , But many people will not notice the details of the lexical environment , Including me !
We know ,catch Clause will have an error object e
function test(value) {
var a = value;
try {
console.log(b);
// Directly refer to a variable that doesn't exist , Will be submitted to the ReferenceError
} catch(e) {
console.log(e, arguments, this)
}
}
test(1);
stay catch Clause arguments, Just to prove catch Clause is not a function . Because if catch It's a function , Obviously it's printed here arguments It shouldn't be test Functional arguments. since catch Not a function , So why can one be limited to catch The error object accessed in clause e?
The answer is catch Clause use NewDeclarativeEnvironment Created a new lexical environment (catch The external lexical environment of the Lexical Environment in clause refers to the function test The lexical environment of ), And then through CreateMutableBinding and SetMutableBinding Will identifier e Associated with the environmental record of the new lexical environment .
Global environment (The Global Environment) It's a special lexical environment , stay ECMAScript Code is created before it is executed . Environmental records in the global environment (Environment Record) It's an object environment record (object environment record), It is bound to a global object (Global Object) On , Embodied in the browser environment , And Global Object The connection is window object .
The global environment is a top-level lexical environment , So the global environment no longer has an external Lexical Environment , In other words, the reference of its external lexical environment is null.
Execution context
When program control shifts to ECMAScript Executable code (executable code) when , You enter an execution context (execution context), The execution context is a logical stack structure (Stack). The top execution context in the stack is the running execution context .
Many people may have doubts about executable code again ,javascript Isn't it all executable code ? No, it isn't , For example, comments (Comment), Blank character (White Space) It's not executable code .
Creation of execution context
We know , Explain to perform global code Or use eval function, Calling a function creates a new execution context , The execution context is a stack structure .
When the control program enters the execution context , This is going to happen 3 An action :
this The value of the keyword is set .
meanwhile VariableEnvironment( constant ) and initial LexicalEnvironment( It could change , So this is about initial) Defined .
Then perform the declarative binding initialization operation .
The execution details of these actions depend on the type of code ( It is divided into global code, eval code, function code Three types of ).
Closure
The necessary condition for closure is :
There are nested functions ;
Nested inner functions must refer to variables defined in the outer functions ;
Nested inner functions must be executed .
The function of closures
Function execution creates a completely new private context , This context may be released , It may not be released , Whether released or not , Its function is :
Protect : Divide a separate code execution area , In this area, you have your own private variable storage space , There will be no conflict between the private variables used and the variables in other areas ( Prevent contamination of global variables )
preservation : If the context is not destroyed , Then the value of the stored private variable will not be destroyed , Can be retrieved from its subordinate context.
版权声明
本文为[Front end sweeper]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221245580380.html
边栏推荐
- The difference between let and VaR
- 数学——协方差
- R language changes dataframe data from long to wide: spread function of tidyr package and pivot of CDATA package_ to_ Rowrecs function, data Table package dcast function
- VNC连接Too many security failures
- JS输出随机数
- Share a recent tag
- .NET宝藏API之:OutputFormatter,格式化输出对象
- 从结构化上下文解读闭包
- 抛物线型压力作用下悬臂梁的abaqus py命令流
- VR panorama truly restores the driving school environment, and VR live shows the hard power of the driving school
猜你喜欢

数学——协方差

企业级代码静态测试工具Helix QAC——技术规格

VR panorama truly restores the driving school environment, and VR live shows the hard power of the driving school

ABAQUS model of local stress of bottom plate and web under the action of steel box girder jacking

書城項目注册頁面和郵箱驗證

Today's sleep quality record 76 points

Application case sharing of isolated integrated current sensor ch704 which can measure current above 50A

C语言预处理详解

Ros2 learning notes (V) learn ros2 parameters from turnlesim

分享一下自己最近写项目遇到的小问题
随机推荐
Share a recent tag
TypeError: connection. connect is not a function
vr全景真实还原驾校环境,vr直播展现驾校硬实力
IDE導入項目
微服务的简单部署
What do the nouns a record, MX record and CNAME record mean?
IDE import project
JS基础6
matlab 自动选择不标准桥梁跨径代码(源代码完全版)
钢箱梁顶推作用下底板腹板局部应力 abaqus模型
Under the pressure of sanctions, Russia plans to import electronic products in parallel through friendly countries
Get rid of the "small workshop" of AI production: how to build a cloud native AI platform based on kubernetes
线程相关问题
Detailed explanation and code example of serial communication of 51 single chip microcomputer
VNC连接Too many security failures
缩表牛:最后的狂欢
Is this open source project going to kill me?
What equipment is needed for VR panoramic shooting? What does it do?
【JZ48 最长不含重复字符的子字符串】
Ros2 learning notes (V) learn ros2 parameters from turnlesim