深拷贝,浅拷贝

浅拷贝:

Object.assign()、Array.slice()、JSON.parse(JSON.stringfy(obj))

深拷贝:

function  qCopy(source) {
    if(typeof source!=='object' && source!==null) return source; //基本类型 返回自身
    let target;
    if(Array.isArray(source)){ //兼容数组和对象
        target=[];
    }else{
        target={};
    }
    for(let key in source){
        if(Object.prototype.hasOwnProperty.call(source,key)){
            if(typeof source[key]=='object'){ //属性值为引用类型
                target[key]= qCopy(source[key]);
            }else{
                target[key]=source[key];
            }
            
        }
    }
    return target;
}
原文地址:https://www.cnblogs.com/alaner/p/15125645.html