1 let obj = { 2 name: ‘james‘, 3 age: 23, 4 height: 198, 5 color: [‘red‘, ‘green‘, ‘yellow‘, ‘deeppink‘], 6 firends: { 7 name: ‘xiaoming‘, 8 age: 23 9 }, 10 hope: function () { 11 console.log(‘movies‘); 12 } 13 } 14 15 16 let obj1 = {} 17 function deepClone(target, origin) { 18 let obj1 = target || [], 19 toStr = Object.prototype.toString, 20 arrStr = "[object Array]"; 21 for (var key in origin) { 22 if(origin.hasOwnProperty(key)) { 23 if(typeof(obj[key]) === ‘object‘) { 24 if(toStr.call(origin[key]) === arrStr) { 25 target[key] = [] 26 }else { 27 target[key] = {} 28 } 29 deepClone(target[key], origin[key]) 30 }else { 31 target[key] = origin[key] 32 } 33 } 34 } 35 return target 36 } 37 deepClone(obj1, obj)
1、思路 :
原文:https://www.cnblogs.com/doumian/p/12616824.html