目前我们最常用的,也是最简便的深拷贝方法是JSON.parse(JSON.stringify(A)),但是如何自己实现一个深拷贝呢,一起来看看吧。

function deepClone(obj){
  if(typeof obj != 'object') return;
  const newObj = obj instanceof Array ? [] : {}
  for(var item in obj) {
    newObj[item] = typeof obj[item] == 'object' ? deepclone(obj[item]) : obj[item]; // 使用递归调用
  }
  return newObj;
}
posted on 2022-05-30 16:48  打怪升级小妮子  阅读(126)  评论(0)    收藏  举报