实现深拷贝函数 clone()
作者:互联网
function deepClone(obj){
function isObject(o){
return (typeof o === 'object' || typeof o === 'function') && o !== null
}
if(!isObject(obj)){return obj}
let newObj = Array.isArray(obj) ? [] : {}
for(key in obj){
if(obj.hasOwnProperty(key)){
newObj[key] = isObject(obj[key]) ? deepClone(obj[key]) : obj[key]
}
}
return newObj
}
hasOwnProperty 判断该属性是否为对象的自有属性
标签:function,obj,函数,clone,newObj,key,return,拷贝,isObject 来源: https://www.cnblogs.com/gyh907368/p/16574329.html