【深、浅拷贝】

function shallowCopy(obj){
  if(typeof obj !== 'object' || obj === null)return obj;
  let newObj = Array.isArray(obj) ? [] : {};
  Object.keys(obj).map(key => {
    newObj[key] = obj[key]
  });

  return newObj;
}

function deepClone(obj){
  if(typeof obj !== 'object' || obj === null)return obj;

  let newObj = Array.isArray(obj)? [] : {};
  Object.keys(obj).map(key => {
    newObj[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]
  });

  return newObj;
}

你可能感兴趣的:(前端,javascript,数据库)