手动实现一个深拷贝

39 阅读1分钟

const deepClone = (obj) => {   if (obj === null || typeof obj !== 'object') return obj;   let copy = Array.isArray(obj) ? [] : {};   for (let key in obj) {     if (obj.hasOwnProperty(key)) {       copy[key] = deepClone(obj[key]);     }   }   return copy; }

使用: let newobj=deepClone(obj)