//任何引用数据类型的值都需要挨个遍历,直到取到基本类型的值为止
function deepClone( obj ){
var cloneObj = Array.isArray( obj ) ? [] : {};
// 判断参数obj不能是null
if( typeof (obj) === 'object' && obj !== null ){
//遍历参数obj
for( let key in obj ){
if( typeof( obj[key] )=== 'object' ){
// cloneObj['hobby'] = obj['hobby'] 数组直接赋值不行的
cloneObj[key] = deepClone(obj[key]);
}else{ //基本数据类型的值
cloneObj[key] = obj[key];
}
}
}
return cloneObj;
}
var obj3 = deepClone(obj1);
标签:obj,数据类型,cloneObj,js,deepClone,key,原理,拷贝,null From: https://www.cnblogs.com/bkyjmjweb/p/16732431.html