深拷贝函数

 1             function deepClone(oldData) {
 2                 if(typeof oldData ==='object' && oldData !==null) {
 3                     let res=Array.isArray(oldData)?[]:{}
 4                     for(let k in oldData){
 5                         if(oldData.hasOwnProperty(k)) {
 6                             res[k] = deepClone(oldData[k]);
 7                         }
 8                     }
 9                     return res;
10                 } else {
11                     return oldData
12                 }
13             }
14             
15         const obj = { name: '张三', age: 18,hi:{a:1,b:2,c:{body:1}},fun:function() {} }
16 
17             var obj2=deepClone(obj)
18             console.log(obj2)

 

posted @ 2023-07-18 14:35  font-dev  阅读(10)  评论(0编辑  收藏  举报