var obj1 = {
name: "哈哈",
age: "15",
school: {
primary: "小学",
middle: "中学"
}
}
var obj2 = deepClone(obj1);
function deepClone(obj = {}) {
//检验obj是不是对象和null如果是直接返回
if (typeof obj !== "object" || obj == null) {
return obj
}
//初始化返回的结果
let result
//判断obj是对象还是数组
if(obj instanceof Array){
result=[]
}else{
result={}
}
for(key in obj){
//判断这个key是不是原型的属性
if(obj.hasOwnProperty(key)){
//递归调用,来深拷贝深层内容
result[key] = deepClone(obj[key])
}
}
return result
}