Javascript 什么是时间死区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33198849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:46:15  来源:igfitidea点击:

What is the temporal dead zone?

javascriptecmascript-6constlet

提问by joews

I've heard that accessing letand constvalues before they are initialized can cause a ReferenceErrorbecause of something called the temporal dead zone.

我听说在初始化之前访问letconst值可能会导致 aReferenceError因为称为临时死区的东西。

What is the temporal dead zone, how does it relate to scope and hoisting, and in what situations is it encountered?

什么是时间死区,它与范围和吊装有什么关系,它在什么情况下遇到?

回答by joews

letand consthave two broad differences from var:

let并且与const有两个广泛的区别var

  1. They are block scoped.
  2. Accessing a varbefore it is declared has the result undefined; accessing a letor constbefore it is declared throws ReferenceError:
  1. 它们是块范围的
  2. var在声明之前访问 a会得到结果undefined;访问 aletconst在它被声明之前抛出ReferenceError

console.log(aVar); // undefined
console.log(aLet); // causes ReferenceError: aLet is not defined
var aVar = 1;
let aLet = 2;

It appears from these examples that letdeclarations (and const, which works the same way) may not be hoisted, since aLetdoes not appear to exist before it is assigned a value.

从这些示例中可以看出,let声明(和const,其工作方式相同)可能不会被提升,因为aLet在为其分配值之前似乎不存在。

That is not the case, however—letand constarehoisted (like var, classand function), but there is a period between entering scope and being declared where they cannot be accessed. This period is the temporal dead zone (TDZ).

然而,情况并非如此——let并且const提升(如varclassfunction),但在进入范围和被声明为无法访问它们之间有一段时间。这个时期是时间死区(TDZ)

The TDZ ends when aLetis declared, rather than assigned:

TDZ 在aLet声明而不是被分配时结束:

//console.log(aLet)  // would throw ReferenceError

let aLet;
console.log(aLet); // undefined
aLet = 10;
console.log(aLet); // 10

This example shows that letis hoisted:

此示例显示let已吊装:

let x = 'outer value';
(function() {
  // start TDZ for x
  console.log(x);
  let x = 'inner value'; // declaration ends TDZ for x
}());

Credit: Temporal Dead Zone (TDZ) demystified

信用:时间死区(TDZ)揭开神秘面纱

Accessing xin the inner scope still causes a ReferenceError. If letwere not hoisted, it would log outer value.

x在内部范围内访问仍然会导致ReferenceError. 如果let没有被提升,它会记录outer value

The TDZ is a good thing because it helps to highlight bugs—accessing a value before it has been declared is rarely intentional.

TDZ 是一件好事,因为它有助于突出错误——在声明之前访问一个值很少是故意的。

The TDZ also applies to default function arguments. Arguments are evaluated left to right, and each argument is in the TDZ until it is assigned:

TDZ 也适用于默认函数参数。参数从左到右计算,每个参数都在 TDZ 中,直到它被分配:

// b is in TDZ until its value is assigned
function testDefaults(a=b, b) { }
testDefaults(undefined, 1); // throws ReferenceError because the evaluation of a reads b before it has been evaluated.

The TDZ is not enabled by default in the babel.jstranspiler. Turn on "high compliance" mode to use it in the REPL. Supply the es6.spec.blockScopingflag to use it with the CLI or as a library.

默认情况下,babel.js转译器中未启用 TDZ 。打开“高合规性”模式以在REPL 中使用它。提供es6.spec.blockScoping标志以将其与 CLI 或作为库一起使用。

Recommended further reading: TDZ demystifiedand ES6 Let, Const and the “Temporal Dead Zone” (TDZ) in Depth.

推荐进一步阅读:TDZ 揭秘ES6 Let、Const 和深度中的“时间死区”(TDZ)

回答by ofir_aghai

Hoisting:
let,const,varare all get hoisted process.
(whats mean they go upper and declare in the top of the scope.)

吊装:
letconstvar都是得到吊装的过程。
(这意味着它们在作用域的顶部进行声明。)

Initialisation:

初始化:

  • vargo also through the initial process, and get initial value of undefined.
  • while let,constdidn't go throw the initial process, so their values are still inaccessible, although they already declared. whats put them in temporal dead zone
  • var也经过初始过程,得到 的初始值undefined
  • while let,const并没有抛出初始进程,所以它们的值仍然无法访问,尽管它们已经声明了。是什么把它们放进去temporal dead zone

So in shortly:

所以很快:

hoisting process: var, let, const
Initialisation process: var

吊装过程:var, let,const
初始化过程: var

回答by niranjan harpale

In case of let and const variables, Basically, Temporal Dead Zone is a zone

在 let 和 const 变量的情况下,基本上,临时死区是一个区域

"before your variable is declared",

“在声明变量之前”,

i.e where you can not access the value of these variables, it will throw an error.

即在您无法访问这些变量的值的地方,它会抛出错误。

ex.

前任。

let sum = a + 5;        //---------
//some other code       //         | ------>  this is TDZ for variable a
                        //         |
console.log(sum)        //---------
let a = 5;

above code gives an error

上面的代码给出了一个错误

the same code will not give an error when we use var for variable 'a',

当我们对变量“a”使用 var 时,相同的代码不会给出错误,

ex.

前任。

var sum = a;                            
console.log(sum)     //prints undefined
var a = 5;