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!