深拷贝(手写)

function deepClone(source){
  if(typeof source == ('string'|| 'number')){
    return source;
  }
  if(!source || typeof source != 'object'){
    throw new Error("error arguments!")
  }
  var newSource = source.constructor === Array? [] : {};
  for(var key in source){
    if(source.hasOwnProperty(key)){
      if(typeof source[key] !== 'object'){
        newSource[key] = source[key]
      }else{
        newSource[key] = deepClone(source[key])
      }
    }
  }
  return newSource;
}

你可能感兴趣的:(深拷贝(手写))