当前位置:网站首页>let到底会不会造成变量提升
let到底会不会造成变量提升
2022-07-30 05:41:00 【我爱RMB】
let到底会不会造成变量提升呢?
关于这个问题,大家可以先在心中想想现在自己的答案,我们继续往下看:
今天有个群里有人提了这个问题,大部分人都说不会,但是在我的印象里是觉得会的,但是太多不同的声音,让我也开始怀疑自己,到底会不会提升呢?
于是我又翻起了红宝书,在红宝书(第四版)第26页,有这么一句话:
let 与 var 的另一个重要的区别,就是 let 声明的变量不会在作用域中被提升。
但是在往后翻翻,在第92页又写了这么一句话:
严格来讲,let 在 JavaScript 运行时中也会被提升,但由于“暂时性死区”(temporal dead zone)的 缘故,实际上不能在声明之前使用 let 变量。因此,从写 JavaScript 代码的角度说,let 的提升跟 var 是不一样的。
所以到底存不存在提升呢?
我们先了解下什么是暂时性死区:
通过let或者const声明的变量会在进入块级作用域的时被创建,但是在该变量没有赋值之前,引用该变量JavaScript引擎会抛出错误。这就是“暂时性死区”。
我们再来看看ecma中对let和const的定义:
let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.
我们重点看第二句:
The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated.(这些变量是在实例化包含它们的词法环境时创建的,但在评估变量的词法绑定之前,不得以任何方式访问它们。)
这里再拓展下:在一个执行上下文中,通过 var 声明的变量,在编译阶段全都被存放到变量环境里面了。通过 let 声明的变量,在编译阶段会被存放到词法环境(Lexical Environment)中。
我们再来看这一段代码:
function test(){
console.log(a);
let a = 1;
}
执行test的时候,编译阶段a已经在内存中,为什么提前访问不了?这主要是因为V8虚拟机做了限制,虽然a在内存中,但是当你在let a 之前访问a时,根据ECMAScript定义,虚拟机会阻止的访问。
最后总结下:
在块作用域内,let声明的变量被提升,但变量只是创建被提升,初始化并没有被提升,在初始化之前使用变量,就会形成一个暂时性死区。
边栏推荐
猜你喜欢
随机推荐
pycharm专业版 配置pytest
453.最小操作数使数组元素相等
P3 元宝第六单元笔记
I/O多路复用技术
51.N皇后(回溯法)
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
[详解C语言]一文带你玩转数组
mysql time field is set to current time by default
This dependency was not found:
art-template模板引擎过滤器的使用【入门简单使用篇】
封装Cookie API
argparse —— 命令行选项、参数和子命令解析器
三子棋游戏——C语言
面试前需要巩固的算法知识点(自用,更新中)
初识C语言
Qt通过QSttings类读取*.ini配置文件
cnpm installation steps
Redis简单了解
Ranking of grades (Huazhong University of Science and Technology postgraduate examination questions) (DAY 87)
Falling ants (Peking University entrance exam questions)









