最简单版本,适用于大多数场景: JSON.parse(JSON.stringify(target));
手写简单版:
const deepClone = (target) => {
if(typeof target === 'object' && target !== null){
const cloneTarget = Array.isArray(target) ? [] : {};
for(var prop in target){
if(target.hasOwnProperty(prop)){
cloneTarget[prop] = deepClone(target[prop]);
}
}
return cloneTarget;
}else{
return target;
}
}