1、浅拷贝:

  1. function shallowClone(obj){
  2. let cloneObj = {}
  3. for(let i in obj){
  4. cloneObj[i] = obj[i]
  5. }
  6. return cloneObj
  7. }

2、深拷贝:

  1. function deepClone(obj){
  2. var result
  3. if(typeof obj === 'object'){
  4. result = obj.constructor === Array?[]:{}
  5. for(var i in obj){
  6. result[i] = typeof obj[i] === 'object' ? deepClone(obj[i]):obj[i]
  7. }
  8. }else{
  9. result = obj
  10. }
  11. return result
  12. }