浅拷贝和使用JavaScript数组的深拷贝有什么区别?(复制)

时间:2021-06-08 13:20:03

This question already has an answer here:

这个问题已经有了答案:

According the MDN documentation calling array.slice() will create a shallow copy of the array.

根据调用array.slice()的MDN文档,将创建数组的一个浅拷贝。

See this MDN link for slice().

请参见此MDN链接的slice()。

However, if I run a simple test as such in the console:

但是,如果我在控制台运行一个简单的测试:

var test = [[1,2,3],7,8,9];
var shallow_copy = test.slice();

and inspect shallow_copy, I can see that the entire 2 dimensional array appears to be copied over.

检查浅拷贝,我可以看到整个二维数组被复制过来。

What is the difference between a shallow copy and a deep copy? If I were to guess, I would have called this a deep copy.

浅拷贝和深拷贝之间的区别是什么?如果我猜的话,我就会把这个叫做深度拷贝。

2 个解决方案

#1


20  

To see the difference, try:

要看到区别,试着:

shallow_copy[0][2] = 4;
console.dir(test);

You'll see that test has been modified! This is because while you may have copied the values to the new array, the nested array is still the same one.

您将看到测试被修改了!这是因为虽然您可能已经将值复制到新的数组中,但嵌套的数组仍然是相同的。

A deep copy would recursively perform shallow copies until everything is a new copy of the original.

深度拷贝会递归地执行浅拷贝,直到所有东西都是原始拷贝的新拷贝。

#2


1  

Basically you're just getting a reference to the original variable/array. Changing the reference will also change the original array. You need to loop over the values of the original array and form a copy.

基本上你只是得到了对原始变量/数组的引用。更改引用也将更改原始数组。您需要对原始数组的值进行循环并形成一个副本。

Consider this example:

考虑一下这个例子:

var orig = {  a: 'A', b: 'B', c: 'C' };

Let's say you want to create a duplicate of this, so that even if you change the original values, you can always return to the original.

假设你想要创建一个这个的副本,这样即使你改变了原始值,你仍然可以返回原始值。

I can do this:

我可以这样做:

var dup = orig; //Shallow copy!

If we change a value:

如果我们改变一个值:

dup.a = 'Apple';

This statement will also change a from orig, since we have a shallow copy, or a reference to var orig. This means, you're losing the original data as well.

这个语句也会从orig中改变a,因为我们有一个浅拷贝,或者是对var orig的引用。这意味着,您也将丢失原始数据。

But, creating a brand new variable by using the properties from the original orig variable, you can create a deep copy.

但是,通过使用来自原始orig变量的属性创建一个全新的变量,您可以创建一个深度副本。

var dup = { a: orig.a, b: orig.b, c: orig.c }; //Deep copy!

Now if you change dup.a, it will only affect dup and not orig.

现在如果你改变dup。a,它只会影响dup,不会影响orig。

#1


20  

To see the difference, try:

要看到区别,试着:

shallow_copy[0][2] = 4;
console.dir(test);

You'll see that test has been modified! This is because while you may have copied the values to the new array, the nested array is still the same one.

您将看到测试被修改了!这是因为虽然您可能已经将值复制到新的数组中,但嵌套的数组仍然是相同的。

A deep copy would recursively perform shallow copies until everything is a new copy of the original.

深度拷贝会递归地执行浅拷贝,直到所有东西都是原始拷贝的新拷贝。

#2


1  

Basically you're just getting a reference to the original variable/array. Changing the reference will also change the original array. You need to loop over the values of the original array and form a copy.

基本上你只是得到了对原始变量/数组的引用。更改引用也将更改原始数组。您需要对原始数组的值进行循环并形成一个副本。

Consider this example:

考虑一下这个例子:

var orig = {  a: 'A', b: 'B', c: 'C' };

Let's say you want to create a duplicate of this, so that even if you change the original values, you can always return to the original.

假设你想要创建一个这个的副本,这样即使你改变了原始值,你仍然可以返回原始值。

I can do this:

我可以这样做:

var dup = orig; //Shallow copy!

If we change a value:

如果我们改变一个值:

dup.a = 'Apple';

This statement will also change a from orig, since we have a shallow copy, or a reference to var orig. This means, you're losing the original data as well.

这个语句也会从orig中改变a,因为我们有一个浅拷贝,或者是对var orig的引用。这意味着,您也将丢失原始数据。

But, creating a brand new variable by using the properties from the original orig variable, you can create a deep copy.

但是,通过使用来自原始orig变量的属性创建一个全新的变量,您可以创建一个深度副本。

var dup = { a: orig.a, b: orig.b, c: orig.c }; //Deep copy!

Now if you change dup.a, it will only affect dup and not orig.

现在如果你改变dup。a,它只会影响dup,不会影响orig。