在ES6中声明为let或const的变量是不是已经挂起? [英] Are variables declared with let or const not hoisted in ES6?

查看:126
本文介绍了在ES6中声明为let或const的变量是不是已经挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩ES6一段时间,我注意到,虽然以 var 声明的变量按预期被提升...

  console.log(typeof name); // undefined 
var name =John;

...用 let const 似乎在提升有一些问题:

  console.log类型名称); // ReferenceError 
let name =John;

  console.log(typeof name); // ReferenceError 
const name =John;

这是否意味着使用声明的变量让 const 未被吊起?这里真的发生了什么?在这件事上, let const 之间有什么区别吗?

解决方案

@thefourtheye在声明之前不能访问这些变量 是正确的。但是,这比这更复杂一些。


使用声明的变量让 const 未提起?这是真的吗?


所有声明 var let const function function * class 已被提升为。这意味着如果一个名称在范围内声明,则在该范围内,标识符将始终引用该特定变量:

  x = 全球; 
//函数范围:
(function(){
x; //不是全局

var / let / ... x;
} ));
//块范围(不适用于`var`s):
{
x; // notglobal

let / const / ... x;
}

功能和块范围 1



var / 函数 / 函数* 声明和 let / const / class declara­ ;itions是初始化

前者已初始化为 undefined 或(生成器)功能,当绑定在范围顶部创建时。然而,词汇声明的变量保持未初始化。这意味着当您尝试访问它时,会引发 ReferenceError 异常。只有当 let / const / class 语句被评估,所有之前(之前)被称为时间死区。

  x = y =全局; 
(function(){
x; // undefined
y; //引用错误:y未定义

var x =local;
let y =local;
}());

请注意, let y; 语句初始化未定义的变量 let y = undefined; 将有。



死区不是句法位置,而是变量(范围)创建和初始化之间的时间。只要该代码未被执行(例如函数体或简单的死代码),引用代码上面的变量就不是错误,如果在初始化之前访问变量,它将抛出异常,即使访问代码在声明之下(例如,在提起的函数声明中被称为太早)。


let const 在这件事?


不,他们的工作一样,只要吊装被认为是。它们之间的唯一区别是, const ant必须是且只能在声明的初始化部分中分配( const one = 1; / code>, const one; 以及稍后重新分配如 one = 2 无效) p>

1: var 声明仍然只在功能级别上工作,当然 p>

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected...

console.log(typeof name); // undefined
var name = "John";

...variables declared with let or const seem to have some problems with hoisting:

console.log(typeof name); // ReferenceError
let name = "John";

and

console.log(typeof name); // ReferenceError
const name = "John";

Does this mean that variables declared with let or const are not hoisted? What is really going on here? Is there any difference between let and const in this matter?

解决方案

@thefourtheye is correct in saying that these variables cannot be accessed before they are declared. However, it's a bit more complicated than that.

Are variables declared with let or const not hoisted? What is really going on here?

All declarations (var, let, const, function, function*, class) are hoisted in JavaScript. This means that if a name is declared in a scope, in that scope the identifier will always reference that particular variable:

x = "global";
// function scope:
(function() {
    x; // not "global"

    var/let/… x;
}());
// block scope (not for `var`s):
{
    x; // not "global"

    let/const/… x;
}

This is true both for function and block scopes1.

The difference between var/function/function* declarations and let/const/class declara­tions is the initialisation.
The former are initialised with undefined or the (generator) function right when the binding is created at the top of the scope. The lexically declared variables however stay uninitialised. This means that a ReferenceError exception is thrown when you try to access it. It will only get initialised when the let/const/class statement is evaluated, everything before (above) that is called the temporal dead zone.

x = y = "global";
(function() {
    x; // undefined
    y; // Reference error: y is not defined

    var x = "local";
    let y = "local";
}());

Notice that a let y; statement initialises the variable with undefined like let y = undefined; would have.

The temporal dead zone is not a syntactic location, but rather the time between the variable (scope) creation and the initialisation. It's not an error to reference the variable in code above the declaration as long as that code is not executed (e.g. a function body or simply dead code), and it will throw an exception if you access the variable before the initialisation even if the accessing code is below the declaration (e.g. in a hoisted function declaration that is called too early).

Is there any difference between let and const in this matter?

No, they work the same as far as hoisting is regarded. The only difference between them is that a constant must be and can only be assigned in the initialiser part of the declaration (const one = 1;, both const one; and later reassignments like one = 2 are invalid).

1: var declarations are still working only on the function level, of course

这篇关于在ES6中声明为let或const的变量是不是已经挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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