js中的深拷贝与浅拷贝
2021-04-03 05:27
                         标签:The   end   就是   oda   es6   style   source   let   jquery      深拷贝:通过递归的方式复制所有的属性;深拷贝就是两者指向不同的内存地址,是真正意义上的拷贝。    lodash很热门的函数库,提供了 lodash.cloneDeep()实现深拷贝。 js中的深拷贝与浅拷贝 标签:The   end   就是   oda   es6   style   source   let   jquery    原文地址:https://www.cnblogs.com/Ky-Thompson23/p/12545600.html深拷贝与浅拷贝
实现浅拷贝的方法
let newArr = [...arr]
let newobj = Object.assign(target, ...others)
实现深拷贝的方法
function deepCopy(target,source){
    for(let index in source){
        if(typeof source[index] === "object"){
            target[index] = {};
            deepCopy(target[index],source[index])
        }else{
            target[index] = source[index];
        }
    }
}
let b = {a:1,d:{b:1}};
let a = {};
deepCopy(a,b);
 a.d.b = 4;
console.log(b)
let newObj = JSON.parse(JSON.stringify(obj))
var array = [1,2,3,4];
var newArray = $.extend(true,[],array);