Home>

You need to understand some basic concepts before understanding deep and shallow copies.The types of variables stored in memory are divided into value types and reference types.

1.Storage characteristics of value type assignment, Copy all the data in the variable, Stored in a new variable.

For example:var num=123;var num1=num;

Indicates that the number stored in the variable is 123. Then copy the data,Just copy 123. Then there are 2 arrays in memory;assigning the copied data to num2 is characterized by having two data copies in memory.This can be understood as a shallow copy.

2. Assignment of reference types.

var o={name:"Zhang San"};

var obj=o;

Assignment is to make a copy of the data stored in variable o, This data is then assigned to obj. There is 1 point of data in memory. The name attribute modified with obj will affect the name in o.

If copying, Make a copy of all reference structures of the data, Then independent data in memory is a deep copy;

If copying, Copy only the attributes of the current object, And the attribute is a reference type is not considered, Then shallow copy

Copy:Make a copy. Means copying the object data;

When discussing deep copy and shallow copy, we must ensure that the object's properties are also reference types.

  • Previous Detailed ng-checked instructions for AngularJS introductory tutorial
  • Next ThinkPHP framework implements data addition, deletion and modification