1. JSON.stringify与JSON.parse

  1. function deepClone(data) {
  2. let _data = JSON.stringify(data),
  3. dataClone = JSON.parse(_data);
  4. return dataClone;
  5. };

2 对象与数组形式进深拷贝

  1. function deepClone(source) {
  2. if (!source && typeof source !== 'object') {
  3. throw new Error('error arguments', 'deepClone')
  4. }
  5. const targetObj = source.constructor === Array ? [] : {}
  6. Object.keys(source).forEach(keys => {
  7. if (source[keys] && typeof source[keys] === 'object') {
  8. targetObj[keys] = deepClone(source[keys])
  9. } else {
  10. targetObj[keys] = source[keys]
  11. }
  12. })
  13. return targetObj
  14. }