• <legend id='wAwK2'><style id='wAwK2'><dir id='wAwK2'><q id='wAwK2'></q></dir></style></legend>
      <bdo id='wAwK2'></bdo><ul id='wAwK2'></ul>
    <tfoot id='wAwK2'></tfoot>
  • <small id='wAwK2'></small><noframes id='wAwK2'>

    <i id='wAwK2'><tr id='wAwK2'><dt id='wAwK2'><q id='wAwK2'><span id='wAwK2'><b id='wAwK2'><form id='wAwK2'><ins id='wAwK2'></ins><ul id='wAwK2'></ul><sub id='wAwK2'></sub></form><legend id='wAwK2'></legend><bdo id='wAwK2'><pre id='wAwK2'><center id='wAwK2'></center></pre></bdo></b><th id='wAwK2'></th></span></q></dt></tr></i><div id='wAwK2'><tfoot id='wAwK2'></tfoot><dl id='wAwK2'><fieldset id='wAwK2'></fieldset></dl></div>
      1. 为什么`const arg = arg;`会产生“初始化前无法访问"?错误?

        Why does `const arg = arg;` produce a quot;Cannot access before initializationquot; error?(为什么`const arg = arg;`会产生“初始化前无法访问?错误?)
            <tbody id='yw59y'></tbody>
            • <bdo id='yw59y'></bdo><ul id='yw59y'></ul>
                <i id='yw59y'><tr id='yw59y'><dt id='yw59y'><q id='yw59y'><span id='yw59y'><b id='yw59y'><form id='yw59y'><ins id='yw59y'></ins><ul id='yw59y'></ul><sub id='yw59y'></sub></form><legend id='yw59y'></legend><bdo id='yw59y'><pre id='yw59y'><center id='yw59y'></center></pre></bdo></b><th id='yw59y'></th></span></q></dt></tr></i><div id='yw59y'><tfoot id='yw59y'></tfoot><dl id='yw59y'><fieldset id='yw59y'></fieldset></dl></div>

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

                <legend id='yw59y'><style id='yw59y'><dir id='yw59y'><q id='yw59y'></q></dir></style></legend>

                <tfoot id='yw59y'></tfoot>

                  本文介绍了为什么`const arg = arg;`会产生“初始化前无法访问"?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现了以前从未想过的非常奇怪的行为.我不确定这是否与 TDZ 有关,因为我认为 TDZ 是从外部范围到内部范围,而不是像这种情况下相反.注意下面例子中的arg.

                  I found really weird behavior that I never thought about before. I'm not sure if this is related to TDZ because I thought TDZ was about from outer scope to inner scope, not the other way around like this case. Pay attention to arg in below examples.

                  // Works
                  
                  const test = {
                     func: (arg) => {
                        const obj = {
                           foo: arg,
                        }
                        return obj.foo;
                     }
                  }
                  

                  // Error
                  
                  const test = arg => {
                     {
                        const arg = arg; // Cannot access 'arg' before initialization
                     }
                  }
                  

                  推荐答案

                  报错的原因是letconst声明都是block-scoped,这意味着 它们只是可在围绕它们的 { } 中访问.因此,由于 constlet,如果另一个 variable (arg) 将无法访问外部范围的 variable (arg) 在块范围内被定义.

                  The reason for the error message is that let and const declarations are both block-scoped, which means that they are only accessible within the { } surrounding them. So because of const or let the variable (arg) from the outer scope won't be accessed if another variable (arg) inside a block scope gets defined.

                  或者换句话说:括号花括号内的变量argarg 你传递给函数是因为你在里面使用了 letconst.

                  Or in other words: The variable arg inside a parentheses or curly brackets is not the same as the arg you pass to the function because you make use of let or const inside.

                  在解析块作用域时,引擎已经为内部定义的每个变量保留了名称.但它们只能在使用 const 或 let 声明和评估之后才能访问.

                  While parsing the block scope the engine already reserves the name for EVERY variable defined inside. But they will only be accessible after the declaration and evaluation using const or let.

                  因此,在写入时读取它会导致您看到的错误.

                  So reading it, while writing to it, causes the error you see.

                  var variable;
                  { // [block/env start] 
                     let variable = variable; // ReferenceError: Cannot access 'variable' before initialization
                  } // [block/env end]
                  

                  let variable = variable 期间发生的情况是它必须在将值/引用分配给左侧之前读取右侧,但根据定义,该变量在声明之前不可用,因此它会引发错误.

                  What happens during let variable = variable is that it has to read the right hand side before it assigns the value/reference to the left hand side, but per definition the variable is not available before declaration, therefor it throws the error.

                  另一个例子是:

                  var variable;
                  {
                    console.log(variable); // ReferenceError: Cannot access 'variable' before initialization
                    let variable;
                  }
                  

                  执行顺序与您示例中的分配相似,并引发相同的错误.它不会访问外部 variable,因为另一个 variable 是使用 let/const 在该块范围内定义的.

                  The execution order is similar to the assignment in your example and throws the same error. It won't access the outer variable because another variable gets defined inside that block scope using let/const.

                  您还可以查看 Let 和 Const 声明.

                  let 和 const 声明定义了范围为正在运行的执行上下文的 LexicalEnvironment 的变量.变量在包含环境记录的实例化时创建,但在评估变量的 LexicalBinding 之前不得以任何方式访问. 由具有 Initializer 的 LexicalBinding 定义的变量被分配其 Initializer 的 AssignmentExpression 的值在评估 LexicalBinding 时,而不是在创建变量时.如果 let 声明中的 LexicalBinding 没有 Initializer,则在评估 LexicalBinding 时,会为变量分配 undefined 值.

                  let and const declarations define variables that are scoped to the running execution context's LexicalEnvironment. The variables are created when their containing Environment Record is instantiated but may not be accessed in any way until the variable's LexicalBinding is evaluated. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer's AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.

                  这篇关于为什么`const arg = arg;`会产生“初始化前无法访问"?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)
                    • <bdo id='pRSxM'></bdo><ul id='pRSxM'></ul>

                        <tfoot id='pRSxM'></tfoot>

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

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

                              <tbody id='pRSxM'></tbody>
                            <legend id='pRSxM'><style id='pRSxM'><dir id='pRSxM'><q id='pRSxM'></q></dir></style></legend>