深拷贝 实现

深度拷贝的实现

//1. 判断数据的类型
function
checkType(target){ return Object.prototype.toString.call(target).slice(8,-1) } //2.定义拷贝函数 function clone(target){ let result,targetType =checkType(target); if( targetType==='Object'){ result = {}; }else if(targetType ==="Array"){ result = []; }else{ return target; } //遍历数据结构中的每一项值 for(let i in target){ let value = target[i] if(checkType(value)==='Object' || checkType(value)==='Arrary'){ //继续遍历获取到的值 clone(value) }else{ result=value } } return result; }

猜你喜欢

转载自www.cnblogs.com/tsgxj/p/10491466.html