是否使用let或const声明了变量? [英] Are variables declared with let or const hoisted?

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

问题描述

我使用ES6已经有一段时间了,我注意到虽然用var声明的变量已按预期悬挂...

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";

...用letconst声明的变量似乎在提升方面存在一些问题:

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

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

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

这是否意味着不提升用letconst声明的变量?这到底是怎么回事? letconst在这件事上有什么区别吗?

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的正确说法是,在声明这些变量之前不能对其进行访问.但是,要复杂得多.

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

是否没有悬挂用letconst声明的变量?到底是怎么回事?

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

所有声明(varletconstfunctionfunction*class)在JavaScript中悬挂" .这意味着,如果在作用域中声明了名称,则在该作用域中,标识符将始终引用该特定变量:

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

对于函数作用域和块作用域而言都是如此 1 .

This is true both for function and block scopes1.

var/function/function*声明与let/const/class声明之间的区别是初始化.
当在范围的顶部创建绑定时,可以使用undefined或(generator)函数对前者进行初始化.但是,按词法声明的变量保持未初始化.这意味着在尝试访问ReferenceError异常时会抛出该异常.仅在评估let/const/class语句时(之前(上方)的所有内容,称为临时死区 ),才会进行初始化.

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

请注意,let y;语句会像let y = undefined;一样使用undefined初始化变量.

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).

letconst在这件事上有什么区别吗?

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

不,就吊装而言,它们的作用相同.它们之间的唯一区别是const ant必须并且只能在声明的初始化程序部分中分配(const one = 1;const one;和以后的重新分配(如one = 2都是无效的)).

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声明仍仅在功能级别上起作用

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

这篇关于是否使用let或const声明了变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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