当前位置:网站首页>Analysis notes on let (const) temporary dead zone in JS
Analysis notes on let (const) temporary dead zone in JS
2022-07-28 22:28:00 【A little square】
let Temporary dead zone of
const And let In this place, the principle is almost , Therefore, in order to let Take an example for analysis
1. What is a temporary dead zone
Let's start with a piece of code
console.log(a); // undefined
var a = 123;
If you know Variable Promotion , Then it's easy to know ,var This code for declaring variables is equivalent to
var a
console.log(tmp); // undefined
a = 123;
But the same code uses let When a variable is declared Report errors
console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a = 123;
This is because of the neglect of let Of Temporary dead zone ( In the above code console.log(a) Medium a Is in a temporary dead zone )
ES6 Regulations ,let/const The command makes the block form a closed scope . If you use variables before declaration , You're going to report a mistake .
All in all , In code block , Use let Before a command declares a variable , This variable is not available .
This is in grammar , be called “ Temporary dead zone ”( temporal dead zone, abbreviation TDZ)
What people often remember is let The declared variables must be used after declaration That's why
2. In a temporary dead zone ‘ Range ’
ES5 Only global scope and function scope , There is no block-level scope , This brings a lot of unreasonable scenes .
ES6 in let It's actually JavaScript Added Block level scope .
Let's look at the following 2 Segment code , understand let/const The command makes the block form a closed scope
var a = 123;
if (true) {
console.log(a); // 123
}
var a = 123;
if (true) {
console.log(a); // ReferenceError: Cannot access 'a' before initialization3
let a
}
In the 2 In this code , Used let,let/const The command makes the block form a closed scope , namely
if (true) {
// let The closed scope of the command line
}
Therefore, in if outside , use var Declared variables , Will be let Scope takeover of .
So where in this scope is Temporary dead zone Well , It's very simple , In this scope let Before declaring a variable , Are the temporary dead zones of this variable .
var a = 123;
if (true) {
// Variable a The temporary dead zone begins
console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a;
// Variable a The temporary dead zone begins
console.log(a); // undefined
a = 123;
console.log(a); // 123
}
3. The impact of a temporary dead zone ( Use let What to pay attention to when )
- let The declared variable must be in Use... After declaration , Otherwise, the report will be wrong
- let Not allowed in the same scope , Repeatedly declare the same variable
- typeof It's no longer a 100% safe operation
On the third point , See the following code
typeof a // ReferenceError: Cannot access 'a' before initialization
let a
边栏推荐
- Ruiji takeout - background login function development
- SQL注入 Less42(POST型堆叠注入)
- [LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
- Openresty request authentication
- Idea generate class diagram plug-in UML (super detailed)
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- Hcip experiment (15)
- 什么是时间复杂度
- What testing services do third-party software testing institutions provide? Charging standard of software test report
- 2021数学建模B组练习
猜你喜欢

HCIP第七次实验

MySQL built-in functions

Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)

HCIP(12)

Bugku, Web: all filtered

SQL injection less34 (post wide byte injection + Boolean blind injection)

HCIP(15)

CDN工作原理

Day3 classification management of Ruiji takeout project

XXX port is already in use
随机推荐
[CS231N]Lecture_ 2:Image Classification pipelin
openresty 请求鉴权
内网渗透学习(三)域横向移动——计划任务
gprs网络指的是什么
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
DHCP and PPPoE protocols and packet capture analysis
容器化配置启动redis集群 单机6节点 3主3从
Sword finger offer II 066. sum of words (medium prefix tree design string)
成立不到一年!MIT衍生量子计算公司完成900万美元融资
Ecmasript 5/6 notes
TensorFlow Serving 高性能的机器学习模型服务系统
HCIP(9)
(翻译)图技术简明历史
SQL injection less42 (post stack injection)
HCIP(13)
SQL注入 Less38(堆叠注入)
[binary tree] pseudo palindrome path in binary tree
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
静态路由和缺省路由实验
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)