Layman understood. Deep copy. Shallow copy

Foreword

Wikipedia definition: copy is the copy of the object pointed pointer , meaning that: pointer to the source object and copy out of the target object at the memory space is the same piece of space, just a simple shallow copy of a copy, make public several objects a memory, however, when memory is destroyed, all pointers to this memory space need to be redefined, or will cause the wrong target.

Copy content summary

all image

Relational data types and stack

Basic types and reference types

The basic types: undefined, null, Boolean, String, Number, Symbol

. Reference types: Object, Array, Date, Function, RegExp, etc.

Storage

. Basic types: primitive values ​​in memory occupied by the fixed size, the stack stored in a memory (not included in the closure variable)

all image

. Reference types: reference value type of the object is stored in the heap memory. And stack memory is stored in a variable object identifier and the object in the heap memory storage address (reference), the reference data type is stored in the stack pointer, the pointer to the start address of the stack entity. When the interpreter to find reference value, it will first retrieve its address on the stack, obtained from the heap after obtaining the address of the entity.

. all imgage

note:

  1. Closure of variables are not stored in the stack memory, but stored in the heap memory. This is relatively miss, if the closure of variables stored in the memory stack, as a function of the outer layer of the destruction from the call stack, variables will certainly be destroyed, but if stored in a heap memory, memory function still have access to the variables in the outer function has been destroyed. See the next section of code corresponding understood:
function A() {
  let a = 'koala'
  function B() {
      console.log(a)
  }
  return B
}

FIG replication with basic data types:

all image

Conclusion: When the data in the stack memory, the data changes occur, the system will automatically assign a new value of a new variable in the stack memory, the two independent variables, affect each other.

Reference data types copy

Look at a piece of code

let a = {x:'kaola', y:'kaola1'}
let b = a;
b.x = '程序员成长指北';
console.log(a.x); // 程序员成长指北

Reference type of replication, the same new variable b is assigned a new value, an error in the stack memory, except that the specific value of this variable is not corresponding to the stack, the stack pointer address only. The same two variable address pointer, pointing object heap memory, so when the change bx, AX also changed.

Shallow copy

Shallow copy is defined:

I do not know api generally prefer to see MDN, the concept of shallow copy of MDN official did not give a clear definition, but the search to a function Array.prototype.slice, the official said that it can achieve a shallow copy of the original array.
For the official to the conclusion that we verify through a piece of code, and concluded that the definition of shallow copy.

var a = [ 1, 3, 5, { x: 1 } ];
var b = Array.prototype.slice.call(a);
b[0] = 2;
console.log(a); // [ 1, 3, 5, { x: 1 } ];
console.log(b); // [ 2, 3, 5, { x: 1 } ];

As can be seen from the output results, the shallow copy, the array a [0] and not with b [0] is changed to change, description addresses a and b are not the same references in the stack memory.

Shallow copy is defined:

Through this official copy function analysis of slice shallow shallow copy definitions:

新的对象复制已有对象中非对象属性的值和对象属性的引用。如果这种说法不理解换一种一个新的对象直接拷贝已存在的对象的对象属性的引用,即浅拷贝。

Deep copy operation

Said assignment and shallow copy operations, we are not able to think of what is already a deep copy, said the following direct deep copy of the definition.

Deep copy of the definition

深拷贝会另外拷贝一份一个一模一样的对象,从堆内存中开辟一个新的区域存放新对象,新对象跟原对象不共享内存,修改新对象不会改到原对象。

Guess you like

Origin www.cnblogs.com/caominjie/p/11456243.html