Good evening, then , I'm the keynote teacher this evening , I'm brother rabbit .


We are often asked the difference between light copy and deep copy in the interview , It happens that some people in the group are also asking this question , So today I will write a blog to explain it .


1. Shallow copy

Shallow copy refers to , Copy the value of the base type , And the address copy of the object type .

Well? , What are basic types ?

stay js in , The basic types we know are strings , number , Boolean value .

When we write such code , It's a shallow copy .
var a = 1; var b = a; // Shallow copy b = 2; // change b Value of , Will not affect a, Because a shallow copy is a value copy for a basic type
a still 1, and b It doesn't matter .

This truth , The same applies to objects .JavaScript There are also object types in ( Also called reference data type ), A shallow copy of an object type simply copies the address .
var p1 = { name: 'jack' } var p2 = p1; = 'rose'; console.log(p1);
p2 Saved are and p1 Memory as like as two peas , It's like you matched a key for Zhang San , With the key, Zhang San can go to your house and drink the yogurt in the fridge .

2. Deep copy

Deep copy means , In addition to copying values of basic types , It also completely reproduces the object type .

An object is fixed in memory , If we want to make a deep copy of it , The only way is to create a new object , The value inside completely reproduces the original object .

Or the example just now , Let's make a little transformation
var p1 = { name: 'jack' } var p2 = { name: }; = 'rose';
thus , We're right p2 Carry out transformation , It won't affect p1 Yes . This is the deep copy .

It is possible for us to implement deep copy in this way , But if there are too many attributes , It will be cumbersome . Here's how to implement the general method of deep copy ?

3. Deep copy  Object.assign()

Is a way to make deep copies of non nested objects , If nesting occurs in an object , Then its behavior to the nested object becomes a common shallow copy . If not nested , This method can be used .
var p1 = { name: 'jack' } var p2 = {} Object.assign(p2,p1);
There is a similar approach , Just use JSON Convert
var p1 = { name: 'jack', age:12 } var p2 = JSON.parse(JSON.stringify(p1)); = 'rose';
In actual development , Maybe this way is used more , For example, turn some data into json Store to local cache , When needed , We'll deserialize it .

4. Deep copy recursion
function deepCopy(dest,src){ var dest = dest || {}; for(var key in src){
// If the attribute of the object is an object , Recursive processing if(typeof src[key] === "object"){ dest[key]=
(src[key].constructor === Array)?[]:{}; deepCopy(dest[key],src[key]); }else{
dest[key]=src[key]; } } return dest; }
test :
var p1 = { name: 'jack', age:12, toy: { name:'car' } } var p2 =

Can see , operation p2 Will not affect p1, This is a deep copy of recursion . 

That's all for today's sharing , You are also welcome to our study qun Communication ~~

©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform