


Detailed introduction to the concept of 'temporary Zenless Zone Zero' in JS
Temporary dead zone
As long as the let
command exists in the block-level scope, the variables declared by it will be "binding" in this area. No longer affected by external influences.
var tmp = 123; if (true) { tmp = 'abc'; // ReferenceError let tmp; }
In the above code, there is a global variable tmp
, but let
declares a local variable tmp
in the block-level scope, resulting in the following Or bind this block-level scope, so before let
declares the variable, assigning a value to tmp
will report an error.
ES6 clearly stipulates that if there are let
and const
commands in a block, this block will have a closed effect on the variables declared by these commands from the beginning. area. Any use of these variables before declaration will result in an error.
In short, within the code block, the variable is not available until it is declared using the let
command. Grammatically, this is called the "temporary dead zone" (TDZ).
if (true) { // TDZ开始 tmp = 'abc'; // ReferenceError console.log(tmp); // ReferenceError let tmp; // TDZ结束 console.log(tmp); // undefined tmp = 123; console.log(tmp); // 123 }
In the above code, before the let
command declares the variable tmp
, it belongs to the "dead zone" of the variable tmp
.
"Temporary dead zone" also means that typeof
is no longer a 100% safe operation.
typeof x; // ReferenceError let x;
In the above code, the variable x
is declared using the let
command, so before it is declared, it belongs to the "dead zone" of x
. As long as you use An error will be reported when reaching this variable. Therefore, typeof
will throw a ReferenceError
when run.
For comparison, if a variable is not declared at all, using typeof
will not report an error.
typeof undeclared_variable // "undefined"
In the above code, undeclared_variable
is a variable name that does not exist, and the result is "undefined". Therefore, before let
, the typeof
operator was 100% safe and would never report an error. This is no longer true. This design is to help everyone develop good programming habits. Variables must be used after they are declared, otherwise an error will be reported.
Some "dead zones" are hidden and not easy to find.
function bar(x = y, y = 2) { return [x, y]; } bar(); // 报错
In the above code, the reason why an error is reported when calling the bar
function (some implementations may not report an error) is because the default value of parameter x
is equal to another parameter y
, and y
has not been declared at this time, which belongs to the "dead zone". If the default value of y
is x
, no error will be reported because x
has been declared at this time.
function bar(x = 2, y = x) { return [x, y]; } bar(); // [2, 2]
In addition, the following code will also report an error, which is different from the behavior of var
.
// 不报错 var x = x; // 报错 let x = x; // ReferenceError: x is not defined
The error reported by the above code is also due to the temporary dead zone. When using let
to declare a variable, an error will be reported as long as the variable is used before the declaration is completed. The above line belongs to this situation. Before the declaration statement of variable x
is completed, the value of x
is taken, resulting in an error "x is not defined".
ES6 stipulates that variable promotion does not occur in temporary dead zones and let
, const
statements, mainly to reduce runtime errors and prevent this from being used before the variable is declared. variables, leading to unexpected behavior. Mistakes like this are very common in ES5, and now that this provision is in place, it's easy to avoid them.
In short, the essence of the temporary dead zone is that as soon as you enter the current scope, the variable you want to use already exists, but it cannot be obtained. You can only obtain and obtain it until the line of code that declares the variable appears. Use this variable.
Related recommendations:
What is the difference between using js string indexof and search
The above is the detailed content of Detailed introduction to the concept of 'temporary Zenless Zone Zero' in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
