Shallow copy

Shallow copy: Simply copy the outermost properties of an object. The deeper properties of the object are not processed. This will result in the copied object pointing to the same memory as the original object. If one object changes the location of this memory, it affects another object. Serialize an object into a JSON string, and use the json.parse () method to generate a new object from the string, implementing deep copy.

object.assign()

This method can be shallow copy, syntax:

Object.assign(target,... sources)Copy the code

Assign (); assign(); assign(); assign(); assign(); assign()

Extended operator

By extending the operators, you can construct an object while performing shallow copying.

concat

Copy an array

slice()

You can copy by intercepting an array

Implement shallow copy source code:

        const shallowClone = (target) => {
            if (typeof target === 'object' && target !== null) {
                const cloneTarget = Array.isArray(target) ? [] : {};
                for (let prop in target) {
                    if (target.hasOwnProperty(prop)) {
                        cloneTarget[prop] = target[prop];
                    }
                }
                return cloneTarget;
            } else {
                return target;
            }
        }
Copy the code

Deep copy

To make a complete copy of an object from memory to a new object, and create a new space in the heap to store the new object, when the new object does not affect the original object.

JSON.stringify

However, several situations occur with the json.stringify () method: The value of the copied object is function, undefined, symbol, Jjson. Stringify: < span style = “margin: 0pt 0pt 0pt; -Infinity, json. stringify returns null 7, cannot copy object loop application

        function Obj() {
            this.func = function () { alert(90) };
            this.obj = { age: 18 };
            this.arr = [1, 24, 4];
            this.und = undefined;
            this.reg = /123/;
            this.date = new Date();
            this.NaN = NaN;
            this.infinity = Infinity;
            this.sym = Symbol(2);
        }

        let obj0 = new Obj();

        Object.defineProperty(obj0, "innumerble", {
            enumerable: false,
            value: "45678"
        })

        console.log("obj0", obj0)
        let obj1 = JSON.stringify(obj0);
        console.log("obj1", obj1);
Copy the code

Implementation of deep copy source code:

const isComplexDataType = obj => (typeof obj === 'object' || typeof obj === 'function') && (obj ! == null) const deepClone = function (obj, Hash = new WeakMap()) {if (obj.constructor === Date) return new Date(obj) // The Date object returns a new Date object if (obj.constructor === If (hash.has(obj)) return hash.get(obj) let (weakMap) if (hash.has(obj)) return hash.get(obj) let AllDesc = Object. GetOwnPropertyDescriptors (obj) / / traverse incoming parameters all the key features of the let cloneObj = Object. The create (Object. GetPrototypeOf (obj), AllDesc) // Inherit the prototype chain hash.set(obj, cloneObj) for (let key of Reflect.ownKeys(obj)) { cloneObj[key] = (isComplexDataType(obj[key]) && typeof obj[key] ! == 'function') ? deepClone(obj[key], hash) : obj[key] } return cloneObj }Copy the code