let arr = [1,2,3];
function copy(obj){
let newobj = null;
if(typeof(obj) == 'object' && obj !== null){
newobj = obj instanceof Array? [] : {};
for(var i in obj){
newobj[i] = copy(obj[i])
}
}else{
newobj = obj
}
return newobj;
}
console.log(copy(arr)) // [1,2,3]
js实现一个克隆函数
于 2020-01-12 10:12:29 首次发布