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
What is the temporal dead zone?
提问by joews
I've heard that accessing let
and const
values before they are initialized can cause a ReferenceError
because of something called the temporal dead zone.
我听说在初始化之前访问let
和const
值可能会导致 aReferenceError
因为称为临时死区的东西。
What is the temporal dead zone, how does it relate to scope and hoisting, and in what situations is it encountered?
什么是时间死区,它与范围和吊装有什么关系,它在什么情况下遇到?
回答by joews
let
and const
have two broad differences from var
:
let
并且与const
有两个广泛的区别var
:
- They are block scoped.
- Accessing a
var
before it is declared has the resultundefined
; accessing alet
orconst
before it is declared throwsReferenceError
:
- 它们是块范围的。
var
在声明之前访问 a会得到结果undefined
;访问 alet
或const
在它被声明之前抛出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 let
declarations (and const
, which works the same way) may not be hoisted, since aLet
does not appear to exist before it is assigned a value.
从这些示例中可以看出,let
声明(和const
,其工作方式相同)可能不会被提升,因为aLet
在为其分配值之前似乎不存在。
That is not the case, however—let
and const
arehoisted (like var
, class
and 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
被提升(如var
、class
和function
),但在进入范围和被声明为无法访问它们之间有一段时间。这个时期是时间死区(TDZ)。
The TDZ ends when aLet
is 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 let
is 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
Accessing x
in the inner scope still causes a ReferenceError
. If let
were 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.blockScoping
flag 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
,var
are all get hoisted process.
(whats mean they go upper and declare in the top of the scope.)
吊装:let
、const
、var
都是得到吊装的过程。
(这意味着它们在作用域的顶部进行声明。)
Initialisation:
初始化:
var
go also through the initial process, and get initial value ofundefined
.- while
let
,const
didn't go throw the initial process, so their values are still inaccessible, although they already declared. whats put them intemporal 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;