Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - ES6中是否不使用let或const声明的变量?(Are variables declared with let or const not hoisted in ES6?)

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

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

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

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

(...用letconst声明的变量似乎在提升时存在一些问题:)

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?

(这是否意味着不提升使用letconst声明的变量?)

What is really going on here?

(这到底是怎么回事?)

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

(letconst在这个问题上有什么区别吗?)

  ask by Lubo? Turek translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@thefourtheye is correct in saying that these variables cannot be accessed before they are declared.

(@thefourtheye的正确说法是在声明它们之前不能访问这些变量。)

However, it's a bit more complicated than that.

(但是,这要复杂得多。)

Are variables declared with let or const not hoisted?

(是否不使用letconst声明的变量?)

What is really going on here?

(这到底是怎么回事?)

All declarations ( var , let , const , function , function* , class ) are "hoisted" in JavaScript.

(所有声明varletconstfunctionfunction*class在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 scopes 1 .

(对于函数作用域和块作用域1都是如此。)

The difference between var / function / function* declarations and let / const / class declarations is the initialisation .

(var / function / function*声明与let / const / class声明之间的区别是初始化 。)


The former are initialised with undefined or the (generator) function right when the binding is created at the top of the scope.

(前者是在合并范围的顶部创建绑定时使用undefined或(generator)函数初始化的。)

The lexically declared variables however stay uninitialised .

(但是,按词法声明的变量保持未初始化状态 。)

This means that a ReferenceError exception is thrown when you try to access it.

(这意味着当您尝试访问ReferenceError异常时会抛出该异常。)

It will only get initialised when the let / const / class statement is evaluated, everything before (above) that is called the temporal dead zone .

(只有在评估let / const / class语句时(之前(之前)的所有内容,称为时间盲区 ),才会进行初始化。)

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

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

Notice that a let y;

(注意let y;)

statement initialises the variable with undefined like let y = undefined;

(语句用undefined初始化变量,例如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 (eg 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 (eg in a hoisted function declaration that is called too early).

(只要未执行该代码(例如,函数体或简单的死代码),就可以在声明上方的代码中引用该变量不是一个错误,并且,如果您在初始化之前访问变量,即使访问该变量,也会引发异常。代码位于声明下方(例如,在过早调用的提升函数声明中)。)

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

(letconst在这个问题上有什么区别吗?)

No, they work the same as far as hoisting is regarded.

(不,就吊装而言,它们的作用相同。)

The only difference between them is that a const ant 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).

(它们之间的唯一区别在于,必须在声明的初始化程序部分中分配const ,并且只能在const one = 1;部分中分配const one = 1;const one = 1; const one;都为const one;以后的重分配,如one = 2都是无效的)。)

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

(1:当然, var声明仍仅在函数级别上起作用)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...