1、js中判断是数组还是对象
//let newObj=obj instanceof Object ? {}:[];
//let newObj = obj.constructor === Array ? [] : {}
let newObj=Array.isArray(obj)?[]:{};
2、深拷贝
function deepClone(obj){
//let newObj=obj instanceof Object ? {}:[];
//let newObj = obj.constructor === Array ? [] : {}
let newObj=Array.isArray(obj)?[]:{};
if(typeof obj !='object'){
return obj;
}else{
for(let key in obj){
if(obj[key]){
newObj[key]= deepClone(obj[key])
}else{
newObj[key]=obj[key];
}
}
}
return newObj;
}
let a={
a:1,
b:2
}
let b=deepClone(a);
console.log(b);