什么是时间死区? [英] What is the temporal dead zone?

查看:156
本文介绍了什么是时间死区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在声明之前访问 let const 值可能导致 ReferenceError 因为某些东西被称为时间死区



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

解决方案

let const var 有两个广泛的区别:


  1. 他们是块范围

  2. 在声明之前访问 var 具有结果 undefined ;在声明之前访问 let const throws ReferenceError

  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> 



信用:TDZ解密



访问 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:

  1. They are block scoped.
  2. Accessing a var before it is declared has the result undefined; accessing a let or const before it is declared throws 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.

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
}());

Credit: TDZ demystified

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆