const obj = { name: 'shuaige', age: 18, like: ['rap', '唱', '跳'] }
function deepClone(obj) {
if (!obj instanceof Object) return obj
const clone = Array.isArray(obj) ? [] : {}
for (const key in obj) {
clone[key] = obj[key] instanceof Object ? deepClone(obj[key]) : obj[key]
}
return clone
}
const obj1 = deepClone(obj);
const obj2 = Object.assign(obj)
console.log(obj1);//{ name: 'shuaige', age: 18, like: [ 'rap', '唱', '跳' ] }
console.log(obj === obj2); //true
console.log(obj === obj1); //false
js手写函数第四篇-简单深拷贝
![](https://img-home.csdnimg.cn/images/20240711042549.png)