The introduction

Deep copy shallow copy is a classic interview questions, in the actual programming also has a role can not be ignored, this article will help you from 0 to understand the essence of shallow copy deep copy and implementation

Without further ado, let’s start with the code

function clone(data){ if(typeof data === 'symbol'){//Symbol return Symbol.for(data.description); } else if(typeof data! = 'object'){return data; } else if (data instanceof Array){//array return data.map(item=>clone(item)) //return [...data] } else if (data.constructor=== Object){//json let res={} for (let key in data){ res[key]=clone(data[key]) ; } return res; // return {... Data}} else{// System and custom object return data.constructor(data); }}Copy the code

Five data types

1. Basic data types: numbers and strings

2. Arrays and JSON

Let a=[2,4,6] let b=a console.log(b) // let a=[2,4,6] Const a=[2,4,6] const b=[...a] The extension operator copies array A to the side, so array B is a new copy of the array object. When you change the value of array A and array D, array B does not change. There are students here asked, deep copy here? Of course not. Now the target array has only one layer. What if you have a nested array? Const a = [2 [8, 9], 4, 6] const b = [...] a [1]. Push. (5) the console log (a) [1] / /,9,5 [8] the console log (b) [1] / /,9,5 [8] See the second array or point to a array references, inside the same json inside is a layer of a layer of all object processing method is the same as a nested array to use our deep copy method Deep copy to achieve deep copy will use recursive thinking, copy an array layer by layer, otherwise, only the first layer is a new object, The second layer will be an object reference to the target array, and we'll talk about the idea of recursion laterCopy the code

3. System variables

System variables: Using Date as an example, there is a wrapped method in JS to copy system objects: Let date01=new Date(2018,10,8) let date02=new Date(date01) date01.setfullyear (2020) console.log(date02); //Thu Nov 08 2018 00:00:00 GMT+0800Copy the code
Note: New Array() and new Object() cannot be copied using this method,
New Object(json) copies a reference to the Object again
New Array(ARR) will be nested into the ARR Array, rather than copying the ARR Array itself

4. When creating custom objects, consider cloning, it is necessary to manually copy objects through instances

class Person{ constructor(name,age){ if(arguments.length=1&&arguments[0] instanceof Person){ let p=arguments[0] this.name=p.name this.age=p.age }else{ this.name=name this.age=age } } } let p1 =new Person('clh',18) let p2 =new Person(p1) console.log(p2==p1); //flaseCopy the code

5.symbol

ES6 introduces a new primitive data type, Symbol, which represents unique values. The largest use is to define unique attribute names for objects.

Symbol.for() : carries the key value

Symbol.for() is similar to the singleton pattern. First, the global search is performed to see if the registered Symbol has the Symbol value of the string parameter as the name. If so, the Symbol value is returned. And registered in the global environment for search.
let s1=Symbol('a'); let s2=Symbol(s1.description) console.log(s2); For ('a'); //Symbol(a) s1===s2; let s2=Symbol.for(s1.description) console.log(s2); //Symbol(a) s1===s2//falseCopy the code