// 网上找的链接忘了、
function deepCopy(obj) {
if (typeof obj !== 'object') {
// 对基本类型进行判断
return obj;
} else if (Object.prototype.toString.call(obj) === '[object Null]') {
// typeof null = object, 需要单独判断
return obj;
} else {
// 实现对数组及对象的深拷贝
let result = Array.isArray(obj) ? [] : {};
// for in 遍历对象和原型链上的属性和方法
for (let key in obj) {
// 判断是否是对象的自身内容,如果是才去做深拷贝
// hasOwnPropertys()方法返回一个布尔值,判断对象是否包含特定的自身非继承属性
if (obj.hasOwnProperty(key)) {
// 判断是否是对象
if (obj[key] && typeof obj[key] === 'object') {
result[key] = this.deepCopy(obj[key]);
} else {
result[key] = obj[key];
}
}
}
return result;
}
}