这段实现深拷贝的代码为什么会出错呢?
暂时没有考虑会传进去,null,正则对象等
function deepclone(target) {
let Tclass = target.constructor
let cloneObject = new Tclass;
for (let key in target) {
if (target.hasOwnProperty(key)) {
cloneObject[key] = deepclone(target[key])
}
}
return cloneObject
}
let obj = {
name: 'wc',
age: 18,
address: {
city: 'bj'
},
arr: [1, 2, 3]
}
let newobj = deepclone(obj)
console.log(newobj);