JS之对象拷贝

浅拷贝:

function clone (source) {
	let target = {};
	for (let key in source) {
		if (source.hasOwnProperty(key)) {
			target[key] = source[key];
		}
	}
	return target;
}

// 测试用例
var a = {
		  name: "muyiy",
		  book: {
		      title: "You Don't Know JS",
		      price: "45"
		  },
		  a1: undefined,
		  a2: null,
		  a3: 123
	}
var b = clone(a);

a.name = "高级前端进阶";
a.book.price = "55";

console.log(b);

// { 
//   name: 'muyiy', 
//   book: { title: 'You Don\'t Know JS', price: '55' },
//   a1: undefined,
//   a2: null,
//   a3: 123
// }


 

深拷贝:

function deepClone (source) {
	if (!isObject(source)) {
		return source;
	}

	let target = Array.isArray(source) ? [] : {};

	for (let key in source) {
		if (source.hasOwnProperty(key)) {
			if (isObject(source[key])) {
				target[key] = deepClone(source[key]);
			} else {
				target[key] = source[key];
            }
        }
    }

	return target;

}

注: 这种实现方法遇到循环引用会抛出错误;

解决方案很简单,其实就是循环检测,我们设置一个数组或者哈希表存储已拷贝过的对象,当检测到当前对象已存在于哈希表中时,取出该值并返回即可。

 


		function deepClone (source, hash = new WeakMap()) {
			if (!isObject(source)) return source;

			if (hash.has(source)) return hash.get(source);

			let target = Array.isArray(source) ? [] : {};
			hash.set(source, target);

			for (let key in source) {
				if (source.hasOwnProperty(key)) {
					if (isObject(source[key])) {
						target[key] = deepClone(source[key], hash);
					} else {
						target[key] = source[key];
					}
				}
			}
			return target;
		}

 

完美!!!!!!!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值