为什么`const arg = arg;`会产生“初始化前无法访问"?错误?

2023-10-21前端开发问题
16

本文介绍了为什么`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;`会产生“初始化前无法访问"?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5