当前位置:网站首页>Native JS perfectly realizes deep copy

Native JS perfectly realizes deep copy

2022-07-25 23:40:00 front-end technology

Native JS Perfect deep copy

Don't talk much , Go straight to the code :

function deepClone(source) {
    
            let targetObj = source.constructor == Array?[]:{
    };
            for(let keys in source) {
    
                if(source.hasOwnProperty(keys)) {
    
                    if(source[keys] && typeof source[keys]=='object') {
    
                        targetObj[keys] = source[keys].constructor == Array?[]:{
    };
                        targetObj[keys] = deepClone(source[keys]);
                    }else {
    
                        targetObj[keys] = source[keys];
                    }
                }
            }
            return targetObj;
        }

All right. , The code is very simple , If you have questions, you can leave a message below .

原网站

版权声明
本文为[front-end technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207190503234804.html