深拷贝方法

深拷贝

function deepClone(obj) {
	if(obj == null) return obj; // null == undefined
	if(obj instanceof Date) return new Date(obj);
	if(obj instanceof RegExp) return new RegExp(obj);
	if(typeof obj != 'object') return obj;
	let newObj = new obj.constructor;
	for(let key in obj) {
		newObj[key] = deepClone(obj[key]);
	}
	return newObj;
}
参考地址

JSON.stringify复制对象特点
一步步推导出的深拷贝
jquery中extend拷贝方法
Object.create拷贝方法

posted @ 2018-12-11 16:22  bonly-ge  阅读(194)  评论(0编辑  收藏  举报