为什么要更改数组';B';在JavaScript中,扩展到数组';A';(用于定义';B';但此后从未直接修改)?

为什么要更改数组';B';在JavaScript中,扩展到数组';A';(用于定义';B';但此后从未直接修改)?,javascript,arrays,variables,constants,scoping,Javascript,Arrays,Variables,Constants,Scoping,我创建了一个名为“alphabet”的常量,并将其分配给包含字母表前5个字母的数组。然后我想有一个函数,可以为数组中的每个字符添加一个数字,以枚举它们。但是,我不想修改原始“alphabet”数组中的值,所以我在枚举函数中创建了一个“temp”变量,只对其进行了更改。然而,我对“temp”所做的任何更改都扩展到了“alphabet”。我不明白为什么,我想阻止这种情况发生 这是我的代码:(也是) 为什么对“临时”的更改会扩展到“字母表”?我想,既然我将“字母表”定义为一个常数,就不可能修改它。而且

我创建了一个名为“alphabet”的常量,并将其分配给包含字母表前5个字母的数组。然后我想有一个函数,可以为数组中的每个字符添加一个数字,以枚举它们。但是,我不想修改原始“alphabet”数组中的值,所以我在枚举函数中创建了一个“temp”变量,只对其进行了更改。然而,我对“temp”所做的任何更改都扩展到了“alphabet”。我不明白为什么,我想阻止这种情况发生

这是我的代码:(也是)

为什么对“临时”的更改会扩展到“字母表”?我想,既然我将“字母表”定义为一个常数,就不可能修改它。而且,我从不在我的功能中对其进行更改。我只使用它来定义“临时”。有什么方法可以防止这些扩散发生吗

我尝试用一个数值常量而不是数组来做类似的事情,一切都按预期进行了:

const number = 10;

function numChange(n) {
  //'virtualN' gets 'n' to avoid making changes directly on the provided argument.
  let virtualN = n;
  //modify 'virtualN' multiple times to emulate what was done to the 'temp' array in the alphaPosition function.
  for (let i = 1; i <= 5; i++) {
    virtualN = "iteration" + i;
  }
  return virtualN;
}

console.log(
  "Step 1. See the value of 'number' before running the numChange function:"
);
console.log(number);
console.log(
  "Step 2. This is the final value of 'virtualN' in 'numChange(number)' after running the function. As expected, it's been modified from its initual value by the 'for' loop:"
);
console.log(numChange(number));
console.log(
  "Step 3. Finally, we can see the value of 'number' is still the same as before running the numChange function. As expected, only the value of virtualN changed while the argument 'n' suffered no modifications:"
);
console.log(number);
为什么使用中间变量来避免对数字而不是数组的原始工作进行更改

我将非常感谢在这方面的任何帮助或澄清。如果你想调整它或者用它做更多的实验。
提前感谢您的帮助。

这是关于如何在javascript中工作的作业。给定以下代码:

const a={foo:bar};
常数b=a;
变量
a
b
都指向同一个对象。这意味着内存中只有一个对象,当您尝试使用
b
访问该对象时,将反映对象
a
点的变化,反之亦然。例如:

const a={foo:bar};
常数b=a;
a、 foo=“baz”;
控制台日志(a);

控制台日志(b)这似乎是的副本。 根本的答案是

我将在下面解释原因:

对于字符串和数字,javascript似乎是按值传递的,而数组等对象则是按引用传递的

这意味着在你的第一个例子中;函数引用原始数组,当您执行
let temp=seq
时,temp实际上只是一个指向传入的原始对象的指针。在这种情况下,这是
字母表
,因此当您修改temp时;它实际上是在修改字母表

Pass by value只是将值发送给函数,因此原始变量与数字示例中的相同

为了获得预期的结果,您需要制作数组的深度副本,如
let temp=deepCopy(seq)


使用
const
我认为可能只是为了让用户知道不要修改它的语法,有些编辑器不允许您在代码中重新修改const,但在这种情况下,它是以一种迂回的方式发生的。

let temp=seq
不创建副本,因为
seq
不包含类似
3
的基本值,而是对内存中对象的引用。(还要注意,将数组声明为常量并不意味着您不能推送到它或更改它的元素,您只是不能覆盖常量实际存储的内容:对内存对象的引用)重复:数组和对象赋值仍将引用原始变量。字符串和数字是基本数据结构,因此赋值是一个值,而不是引用。在将数组或对象分配给新变量之前,您需要创建一个副本。非常感谢,@Nick!替换<代码>让温度=顺序
让温度=[…顺序]成功了。我第一次尝试使用大括号
{
}
而不是括号
[
]
(如您的示例中所示)来创建副本,但是我得到了
{0:a',1:b',2:c',3:d',4:e}
而不是预期的
[“a1”,“b2”,“c3”,“d4”,“e5”
。我从未见过这样的数组(或对象?)格式,我甚至无法访问带有索引括号的分号后面的数字
temp[1]
,等等。那是什么格式?我似乎无法像控制阵列一样控制它。你能给我指出正确的方向吗?是的,当然,
{}
是一个对象,它允许键值对
[]
是一个数组,它是数字索引的。从技术上讲,它们只是JavaScriptAh中不同类型的“对象”,明白了吗!非常感谢您的解释@Nick。感谢您的回复@xPaillant!我不知道是谁否决了你,但你的解释对我很有帮助。谢谢不过,有一点需要澄清,当我运行deepCopy(seq)时,它返回一个“deepCopy()”未定义错误。我想你的意思是这段话只是为了说明我必须做什么,对吗?@Marcos,很高兴你发现它很有用!deepCopy实际上并不是一个完美的功能!谢谢你的澄清。
/*
-> Step 1. 'alphabet' array before running the 'alphaPosition' function:
-> ["a", "b", "c", "d", "e"]
-> Step 2. This is the final value of 'temp' in 'alphaPosition' after running the function. An index has been added to every element in the array, as expected:
-> ["a1", "b2", "c3", "d4", "e5"]
-> Step 3. Here's the 'alphabet' array after running 'alphaPosition'. Indexes have also been added to every element, despite not modifying the function argument directly:
-> ["a1", "b2", "c3", "d4", "e5"]
*/
const number = 10;

function numChange(n) {
  //'virtualN' gets 'n' to avoid making changes directly on the provided argument.
  let virtualN = n;
  //modify 'virtualN' multiple times to emulate what was done to the 'temp' array in the alphaPosition function.
  for (let i = 1; i <= 5; i++) {
    virtualN = "iteration" + i;
  }
  return virtualN;
}

console.log(
  "Step 1. See the value of 'number' before running the numChange function:"
);
console.log(number);
console.log(
  "Step 2. This is the final value of 'virtualN' in 'numChange(number)' after running the function. As expected, it's been modified from its initual value by the 'for' loop:"
);
console.log(numChange(number));
console.log(
  "Step 3. Finally, we can see the value of 'number' is still the same as before running the numChange function. As expected, only the value of virtualN changed while the argument 'n' suffered no modifications:"
);
console.log(number);
/*
-> Step 1. See the value of 'number' before running the numChange function:    
-> 10     
-> Step 2. This is the final value of 'virtualN' in 'numChange(number)' after running the function. As expected, it's been modified from its initual value by the 'for' loop:  
-> iteration5
-> Step 3. Finally, we can see the value of 'number' is still the same as before running the numChange function. As expected, only the value of virtualN changed while the argument 'n' suffered no modifications:
-> 10
*/