Object.assign Merge objects belong to shallow copy. Only one layer can be merged, but deep copy cannot be implemented.

To implement deep copy, you can use the following methods:

function deepAssign() {
    let name, options, src, copy
    let length = arguments.length
    // Record the subscript of the object to be copied
    let i = 1
    // target is the first argument by default
    let target = arguments[0) | | {}// If target is not an object, we cannot copy it, so set it to {}.
    if (typeoftarget ! = ='object') {
        target = {}
    }
    // Loop over the object to be copied
    for (; i < length; i++) {
        // Get the current object
        options = arguments[i]
        // The request cannot be null to avoid extend(a,,b)
        if(options ! =null) {
            for (name in options) {
                // Target attribute value
                src = target[name]
                // The property value of the object to copy
                copy = options[name]

                if (copy && typeof copy == 'object') {
                    // recursive call
                    target[name] = deepAssign( src, copy)
                } else if(copy ! = =undefined) {
                    target[name] = copy
                }
            }
        }
    }

    return target
}
Copy the code

Use as follows:

The code is implemented in reference to the extend implementation in Jquery.

Refer to the link

  • stackoverflow
  • Lot – $. The realization of the extend