js 深拷贝

const obj1 = {
    name: 23,
    address: {
        city: 'beijing'
    },
    num: [1, 2, 3]
};
const obj2 = obj1;
obj2 === obj1 // true
const obj3 = JSON.parse(JSON.stringify(obj1))
obj3 === obj1 // false

 封装方法递归

 

/**
 * 深拷贝
 * @param {Object} obj 要拷贝的对象
 */
function deepClone (obj = {}){
    if (typeof obj !=='object'|| typeof obj == null) {
        return obj
    }
    let result 
    if(obj instanceof Array){
        result =[]
    }else{
        result ={}
    }
    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            result[key] =deepClone(obj[key])
        }
    }
    return result
}
const obj4 = deepClone (obj1)
obj4 === obj1 //false
obj4 == obj1   //false
原文地址:https://www.cnblogs.com/yuwen1995/p/13038904.html