什么是时间死区? [英] What is the temporal dead zone?
问题描述
我听说在声明之前访问 let
和 const
值可能导致 ReferenceError
因为某些东西被称为时间死区。
什么是时间死区?它与范围和提升有关,在什么情况下遇到?
let
和 const
与 var
有两个广泛的区别:
- 他们是块范围。
- 在声明之前访问
var
具有结果undefined
;在声明之前访问let
或const
throwsReferenceError
:
console.log(aVar) ; // undefinedconsole.log(aLet); //引用ReferenceError:aLet不定义aVar = 1; let aLet = 2;
从这些示例中可以看出, let
声明(和 const
,以相同的方式工作)不是已悬挂,因为 aLet
在分配值之前似乎不存在。
然而,不是这样 - let
和 const
已经悬挂(如 var
, class
和函数
),但是在输入范围和被声明在哪里无法访问之间有一段时间。 此时间段是时间死区(TDZ)。
当 aLet
,而不是分配:
// console.log(aLet)//将抛出ReferenceErrorlet aLet; console.log(aLet); // undefinedaLet = 10; console.log(aLet); // 10
此示例显示让
被提升:
let x =外部值';(function(){//启动TDZ for x console.log(x); let x ='inner value'; //声明结束TDZ for x}());
/ pre>
访问 x
在内部范围仍然导致一个 ReferenceError
。如果 let
未挂起,则会记录外部值
。
TDZ是一件好事,因为它有助于突出显示错误 - 在声明之前访问值很少有意。
TDZ也适用于默认函数参数。参数从左到右进行评估,每个参数都在TDZ中,直到它被分配:
// b在TDZ中,直到它的值被赋值为
function testDefaults(a = b,b){}
testDefaults(undefined,1); //抛出ReferenceError,因为在对其进行评估之前读取b的评估。
TDZ未在默认情况下在 babel.js transpiler。打开高度合规模式,以在 REPL 中使用它。提供 es6.spec.blockScoping
标志将其与CLI或库一起使用。
推荐进一步阅读: TDZ demystified and ES6让,Const和深度的时间死区(TDZ) 。
I've heard that accessing let
and const
values before they are declared can cause a ReferenceError
because of something called the temporal dead zone.
What is the temporal dead zone, how does it relate to scope and hoisting, and in what situations is it encountered?
let
and const
have two broad differences from var
:
- They are block scoped.
- Accessing a
var
before it is declared has the resultundefined
; accessing alet
orconst
before it is declared throwsReferenceError
:
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.
That is not the case, however—let
and const
are hoisted (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).
The TDZ ends when aLet
is declared, rather than assigned:
//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 x = 'outer value';
(function() {
// start TDZ for x
console.log(x);
let x = 'inner value'; // declaration ends TDZ for x
}());
Accessing x
in the inner scope still causes a ReferenceError
. If let
were not hoisted, it would log 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.
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:
// 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.js transpiler. 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.
Recommended further reading: TDZ demystified and ES6 Let, Const and the "Temporal Dead Zone" (TDZ) in Depth.
这篇关于什么是时间死区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!