// Copy: // ------ Copy of simple data type: no deep or shallow copy exists. The value is passed directly in the stack ------------; Var a=10; --var b=a // (1) shallow copy: for complex (reference) data type, copy to stack value; // ------ (array/object) shallow copy address in stack ------------ pass address; Var obj1={id: 123}; --var obj2=obj1(obj1 and obj2 copy the same address in the stack, pointing to the same heap) // (2) Deep copy: for complex (reference) data types (array/object), copy the values from the heap to the new array/object; ------ Complex data type copy heap values are deep copies. (1) Shallow copy Features Only a reference is copied. Modifying the copied data will affect the original data. // (2) Deep copy (object/array) features: When copying, a new copy of data is copied from the heap. Modifying the copy does not change the original data. Var arr1 = [1, 2, 3, 4]; var arr1 = [1, 2, 3, 4]; var arr2 = [...arr1]; arr1[0] = 666; console.log(arr1); // [666, 2, 3, 4] console.log(arr2); // [1, 2, 3, 4] // 2) Array method array.slice ()--------- array.slice (start index, end index) does not modify the Array, but returns a subarray containing the start index to end index "before", Strings also have the slice() method, which is used consistently. var arr1 = [1, 2, 3, 4]; arr2 = arr1.slice(0) arr1[0] = 666; console.log(arr1); // [666, 2, 3, 4] console.log(arr2); / / [1, 2, 3, 4] / / 3) Array. The Array method contact () -- -- -- -- -- -- -- -- - Array. Contact (arrays 1, 2... / array element 1, element 2... Instead of modifying an array, an contained subarray is returned. Strings also have the slice() method, which is used consistently. var arr1 = [1, 2, 3, 4]; arr2 = arr1.concat() arr1[0] = 666; console.log(arr1); // [666, 2, 3, 4] console.log(arr2); // [1, 2, 3, 4] // 1) Object.assign(obj) Copy: When there are only level-1 attributes in an Object, this method is a deep copy. When there are objects in an Object, this method is a shallow copy after the level-2 attributes. -- -- -- -- -- -- -- -- -- -- Object. The assign (target,... sources); Var obj = {name: 'zs', // brother: {name: 'ls', // friend: {name: Var obj1 = object.assign (obj); console.log(obj1); // 2) Encapsulating function copy: for(var k in obj) traverses the object, realizes assignment copy of simple data types, determines data types by conditions, and realizes deep copy by combining function recursion, thus achieving deep copy. / /!!!!!! By determining the data type, you can make (deep) copies of all arrays/objects // method 1. Traverse method 1 (same as method 2, Function deepClone(target) {let result if (typeof target === 'object') {if (array.isarray (target)) {result = [] for (let i in target) { result.push(deepClone(target[i])) } } else if (target === null) { result = target } else if (target.constructor === RegExp) { result = target } else if (target.constructor === Date) { result = target } else { result = {} for (let i in target) { result[i] = deepClone(targer[i]) } } } else if (typeof target === 'function') { // If result = new Function('return '+ target.toString())} else { If number, string, Boolean, undefined result = target} return result} Function deepCopy(newObj, oldObj) {for (var k in oldObj) { OldObj [k] var item = oldObj[k]; If (item instanceof Array) {newObj[k] = []; deepCopy(newObj[k], item); } else if (item instanceof Object) { // 3. NewObj [k] = {}; deepCopy(newObj[k], item); } else {newObj[k] = item; }} return newObj} // Method 3 converts to a JSON string and back (serialization -- deserialization), provided that the object to be converted conforms to the format of the ** JSON data type ** : the function does not work. Parse (newData) function deepClone(data) {let newData = json.stringify (data), dataClone = json.parse (newData); return dataClone; }; // test let arr = [1, 2, 3], newData = deepClone(arr); arr[0] = 2; The console. The log (arr, newData) / / (2, 2, 3] [1, 2, 3] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 2021.06.03 released in Denver QiangCopy the code