为什么`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.

相关文章