1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| function checkType(obj){ retrun Object.prototype.toString.call(obj).slice(8, -1); //[object,String] }
function deepClone(target) { let result, targetType = checkedType(target)
if (targetType === 'object') { result = {} } else if (targetType === 'Array') { result = [] } else { return target }
for (let i in target) { let value = target[i] if (checkedType(value) ==='Object' || checkedType(value) ==='Array') { result[i] = deepClone(value) } else { result[i] = value; } }
return result }
|