为什么添加默认参数值会在修改参数时改变行为?

Why does adding a default parameter value change the behavior when modifying the arguments?

第一部分是:

var a = 1;

function fn2(a) {
  arguments[0] = 20;
  var a = 22;
  console.log(a);
  console.log(arguments[0]);
}
fn2(a);

第二部分是:

var a = 1;

function fn2(a, b = 100) {
  arguments[0] = 20;
  var a = 22;
  console.log(a);
  console.log(arguments[0]);
}
fn2(a);

我能理解为什么第一段代码最后会输出22和22,因为arguments[0]a实际上都指向参数a。 但是在第二段代码中(加了一个参数b),最后输出的是22和20,我猜是跟多了一个参数b有关,于是找了一些帖子。在 MDN 中,我发现:

When a non-strict function does contain rest, default, or destructured parameters, then the values in the arguments object do not track the values of the arguments. Instead, they reflect the arguments provided when the function was called

这是来自 MDN 的示例代码:

function func(a = 55) { 
  arguments[0] = 99; // updating arguments[0] does not also update a
  console.log(a);
}
func(10); // 10

我能理解它的意思,但我的问题有所不同。

  1. 在 MDN 示例中,指向 arguments[0]a 具有默认值,而在我的问题中,b 没有t 指向 arguments[0] 有一个默认值。这是 2 个不同的条件。

  2. 我也在控制台调试,发现:

为什么这里有 2 个 a?一个在街区,另一个在本地。那么参数a实际上是哪一个呢?另一个 a(不是参数)来自哪里?

In the MDN example, it is the a which points to arguments[0] has a default value while in my question it is the b which doesn't point to arguments[0] has a default value. These are 2 different conditions.

要么至少一个 rest/default/destructured参数存在,在这种情况下没有分配给[=10=的任何指标] 将导致对关联变量的更改,或者有 no rest/default/destructured 参数,在这种情况下 all 分配给任何索引arguments 将导致关联变量发生变化。

要么 arguments 的每个索引都会更改其关联变量,要么 none 会。因此,即使只有 b (arguments[1]) 具有默认分配,当 b 具有默认分配时,对 arguments[0] 的更改将导致对 a 的更改,因为嗯。

why here we have 2 a? One is in the block while the other is in the local. So which one is actually the parameter a? And where is another a(which is not the parameter) from?

我很确定 Chrome 调试器只是混淆了,因为你有 var aa 已经作为参数存在于范围中。变量出现在 "block" 部分时,它们是

(1) 在函数的顶层声明(使用 constletvar),或

(2) 在非功能块

中用 constlet 声明

a前面的var,当a已经存在于范围内时,完全是多余的。