当前位置:网站首页>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
边栏推荐
- What are the main functions and uses of LCR tester
- From Web3 to web2.5, is it backward or another way?
- hcip实验(15)
- Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator
- [Ruiji takeout project]day4 - dish management
- Which is the file transfer command in the basic services of the Internet
- HCIP(13)
- Learning notes and summary of C language programming specification
- CDN工作原理
- HCIP第七次实验
猜你喜欢

Idea generate class diagram plug-in UML (super detailed)

Hcip experiment (12)

96. Different binary search trees (medium binary search tree dynamic planning)

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

Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true

Static route and default route experiment

HCIP(11)

Add DNS server to LAN for domain name resolution
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

What are the main functions and uses of LCR tester
随机推荐
HCIP(13)
LVS+KeepAlived高可用部署实战应用
SQL注入 Less38(堆叠注入)
[leetcode] maximum depth of binary tree
Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
纪念一下第一次写的线段树了喽(对应洛谷3372)
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
[Ruiji takeout] day05 package management business development
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
Intranet penetration learning (III) horizontal movement of domain - planning tasks
Written examination summary record
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
HCIP(10)
What is time complexity
HCIP(14)
What testing services do third-party software testing institutions provide? Charging standard of software test report
HCIP(13)
What is the difference between inline elements and block level elements? Semantic function
阿里云CDN实践
MOV格式是不是静态图像文件格式