typeof能判断哪些类型
let obj1 ={
age:20,
name:'wangfeng',
address:{
city:'paries'
},
arr:['1','2','3']
}
console.log(obj1)
let obj2 =deepClone(obj1)
obj2.address.city = 'shanghai'
obj2.arr[0] ='5'
console.log(obj1.address.city)
console.log(obj1.arr[0])
/**
* 深拷贝
* @param {object} obj 要拷贝的对象
*/
function deepClone(obj = {}){
if(typeof obj !=='object' || obj ==null){
// obj 是null,或者不是对象和数据,直接返回
return obj
}
//
let result
if(obj instanceof Array){
result = []
}else{
result = {}
}
for(let key in obj){
// 保证key不是原型的属性
if(obj.hasOwnProperty(key)){
result[key] = deepClone(obj[key])
}
}
return result
}