• <bdo id='mfPeb'></bdo><ul id='mfPeb'></ul>
    1. <small id='mfPeb'></small><noframes id='mfPeb'>

    2. <tfoot id='mfPeb'></tfoot>
    3. <i id='mfPeb'><tr id='mfPeb'><dt id='mfPeb'><q id='mfPeb'><span id='mfPeb'><b id='mfPeb'><form id='mfPeb'><ins id='mfPeb'></ins><ul id='mfPeb'></ul><sub id='mfPeb'></sub></form><legend id='mfPeb'></legend><bdo id='mfPeb'><pre id='mfPeb'><center id='mfPeb'></center></pre></bdo></b><th id='mfPeb'></th></span></q></dt></tr></i><div id='mfPeb'><tfoot id='mfPeb'></tfoot><dl id='mfPeb'><fieldset id='mfPeb'></fieldset></dl></div>
      <legend id='mfPeb'><style id='mfPeb'><dir id='mfPeb'><q id='mfPeb'></q></dir></style></legend>

        是否使用 let 或 const 提升了声明的变量?

        时间:2024-04-19

          <legend id='262uZ'><style id='262uZ'><dir id='262uZ'><q id='262uZ'></q></dir></style></legend>
            <tbody id='262uZ'></tbody>
          <i id='262uZ'><tr id='262uZ'><dt id='262uZ'><q id='262uZ'><span id='262uZ'><b id='262uZ'><form id='262uZ'><ins id='262uZ'></ins><ul id='262uZ'></ul><sub id='262uZ'></sub></form><legend id='262uZ'></legend><bdo id='262uZ'><pre id='262uZ'><center id='262uZ'></center></pre></bdo></b><th id='262uZ'></th></span></q></dt></tr></i><div id='262uZ'><tfoot id='262uZ'></tfoot><dl id='262uZ'><fieldset id='262uZ'></fieldset></dl></div>
          • <small id='262uZ'></small><noframes id='262uZ'>

              • <bdo id='262uZ'></bdo><ul id='262uZ'></ul>
                1. <tfoot id='262uZ'></tfoot>
                  本文介绍了是否使用 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 语句被评估时被初始化,之前(上面)的所有内容都称为 temporal死区.

                  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; 语句使用 undefined 初始化变量,就像 let y = 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?

                  不,就提升而言,它们的工作方式相同.它们之间的唯一区别是 constant 必须并且只能在声明的初始化部分中赋值(const one = 1;,两者都是 constone; 和以后的重新分配,如 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 提升了声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='XUmyO'></bdo><ul id='XUmyO'></ul>

                  • <legend id='XUmyO'><style id='XUmyO'><dir id='XUmyO'><q id='XUmyO'></q></dir></style></legend>
                        <tbody id='XUmyO'></tbody>
                      <i id='XUmyO'><tr id='XUmyO'><dt id='XUmyO'><q id='XUmyO'><span id='XUmyO'><b id='XUmyO'><form id='XUmyO'><ins id='XUmyO'></ins><ul id='XUmyO'></ul><sub id='XUmyO'></sub></form><legend id='XUmyO'></legend><bdo id='XUmyO'><pre id='XUmyO'><center id='XUmyO'></center></pre></bdo></b><th id='XUmyO'></th></span></q></dt></tr></i><div id='XUmyO'><tfoot id='XUmyO'></tfoot><dl id='XUmyO'><fieldset id='XUmyO'></fieldset></dl></div>

                      <small id='XUmyO'></small><noframes id='XUmyO'>

                          <tfoot id='XUmyO'></tfoot>