/**
* 深度克隆
* @param {*} target 拷贝对象
* @returns result 拷贝结果对象
*/
function deepClone(target){
let result;
if(typeof target === 'object'){
if(Array.isArray(target)){ // 如果是数组
result = [];
for(const i in target){
result.push(deepClone(target[i]));
}
} else if(target === null){
result = null;
} else if(target.constructor === RegExp || target.constructor || Date){
result = target;
} else {
let result = {};
for(const i in target){
result[i] = deepClone(target[i]);
}
}
} else { // 拷贝对象为 简单数据类型: number、string、boolean
result = target;
}
return result;
}
前端开发