当前位置:网站首页>Learn Scope from a Compilation Perspective!
Learn Scope from a Compilation Perspective!
2022-07-30 23:35:00 【Mohai laughs】
作用域是什么?
思维导图

编译原理
我们都认为js是一门动态语言,But it's actually a compiled language,But not ahead-of-time compiled,Compilation results are also not available in distributed systems transplant in the system
编译流程
传统编译语言的流程
程序中的一段源代码在执行之前会经历三个步骤,统称为“编译”.
分别是:分词/词法分析,解析/语法分析,代码生成.
1. Word segmentation and lexical analysis:
词法单元:
将由字符组成的字符串分解成(对编程语言来说)有意义的代码块,这些代码块被称为词法单元.
The main difference between tokenization and lexical analysis:
The main difference is whether the recognition of the lexical unit is carried out in a stateful or stateless manner.
2.解析/语法分析
Convert a stream of tokens into an abstract syntax tree.This tree is called an abstract syntax tree(AST).
3.代码生成
将 AST 转换为可执行代码的过程称被称为代码生成
jsThe time node at which the compilation took place
JavaScript 的编译过程不是发生在构建之前的.In most cases compilation happens within a few microseconds before the code is executed.在编译时,jsThe engine will do its best to optimize performance.
理解作用域
角色
引擎
从头到尾负责整个 JavaScript 程序的编译及执行过程.
编译器
负责语法分析及代码生成
作用域
负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非常严格的规则,确定当前执行的代码对这些标识符的访问权限.
var a = 2
其实var a = 2
是执行了2个声明.in the compiler and engine respectively.
First the compiler will perform a normal compilation,也就是词法分析,语法分析.In the final code production will and scope“交流”.对变量进行声明.
而a = 2
This assignment is performed while the engine is running,Variables are looked up by scope,If it is not found at the end, an error will be reported.
总结:变量的赋值操作会执行两个动作,首先编译器会在当前作用域中声明一个变量(如 果之前没有声明过),然后在运行时引擎会在作用域中查找该变量,如果能够找到就会对 它赋值.
LHS和RHS查询
The engine's lookup of variables is divided intoLHS和RHS2种.
LHS A query is an attempt Find the variable's container itself,从而可以对其赋值.
RHSis the source value of the variable.
LHS 和 RHS 的含义是“赋值操作的左侧或右侧”不是“=”
特别注意:函数声明
The compiler can process declarations and value definitions at the same time as code generation,Such as when the engine executes code,There is no thread dedicated to passing a function value“分配给” 一个变量.The function is already assigned at the compiler declaration stage.
引擎和作用域的对话
1.小案例

2.小案例

作用域嵌套
The scope is like a bubble,A large bubble wraps a small bubble,Variables that cannot be found in the small bubble will be found in the large bubble,Look up level by level.
当一个块或函数嵌套在另一个块或函数中时,就发生了作用域的嵌套.因此,在当前作用 域中无法找到某个变量时,引擎就会在外层嵌套的作用域中继续查找,直到找到该变量, 或抵达最外层的作用域(也就是全局作用域)为止.
遍历嵌套作用域链的规则
引擎从当前的执行作用域开始查找变量,如果找不到, 就向上一级继续查找.当抵达最外层的全局作用域时,无论找到还是没找到,search process 会停止.
异常
RHS 查询,找不到就报错
RHS 查询在所有嵌套的作用域中遍寻不到所需的变量,引擎就会抛出 ReferenceError 异常.
LHSIf the query does not find one, declare one
当引擎执行 LHS 查询时,如果在顶层(全局作用域)中也无法找到目标变量, 全局作用域中就会创建一个具有该名称的变量,并将其返还给引擎(非严格模式),Strict mode still reports an error.
Manipulate variables
The engine throws another type of exception,叫作 TypeError.
undefined.xxx
,var a = 3; a()
This is typical abuse.
总结:
ReferenceError 同作用域判别失败相关,而 TypeError 则代表作用域判别成功了,但是对 结果的操作是非法或不合理的.
参考
你不知道的js(上),It's actually a reading note,哈哈哈.
边栏推荐
猜你喜欢
随机推荐
transition过渡&&animation动画
智能创意中的尺寸拓展模块
[Meng Xin problem solving] Delete the Nth node from the bottom of the linked list
可视化工具Netron介绍
$\text{ARC 145}$
uniapp开发微信小程序-软考刷题小程序
Apache Doris系列之:深入认识实时分析型数据库Apache Doris
“蔚来杯“2022牛客暑期多校训练营4 DHKLN
proemthues 服务发现配置
反转链表-就地逆置法
Dry goods | 4 tips for MySQL performance optimization
Reverse linked list - in-place inversion method
Debezium error series 20: task failed to create new topic. Ensure that the task is authorized to create topics
软件测试三阶段,你在哪一步?
BFS题单总结
数据清洗-使用es的ingest
2022牛客暑期多校训练营1 J Serval and Essay
mysql 中手动设置事务提交
Apache Doris series: detailed steps for installation and deployment
Axure轮播图