let obj = { name:"qin", age:123, arr:[1,2,34,5,{ name:"jin", sex:"女" }], obj1:{ ay:[32,4] } } let arr1 = [2,4,2,35,2] function deepClone(origin,target){ //调用对象原型链上面的tostring方法判断数据主体是对象还是数组 let toString = Object.prototype.toString, obj_array = "[object Array]"; target = target || (toString.call(origin) == obj_array ? [] : {});//判断当前源主体是对象还是数组并赋值 for(let prop in origin){//循环主体里面的每一个子元素 if(origin.hasOwnProperty(prop)){//判断prop属性是不是origin自带的 if(typeof(origin[prop]) == "object"){//如果是object类型 target[prop] = (toString.call(origin[prop]) == obj_array ? [] : {}); deepClone(origin[prop],target[prop]);//如果子元素是引用数据的话就递归 } else{ target[prop] = origin[prop];//如果子元素是基本数据类型的话就直接赋值 } } } return target; } let result = deepClone(obj); let result1 = deepClone(arr1); console.log(result,result1);