手写深拷贝

Posted missguolf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手写深拷贝相关的知识,希望对你有一定的参考价值。

function isObject(obj) {
return Object.prototype.toString.call(obj) === ‘[object Object]‘
}
function deepCopy(source,hash = new WeakMap()){
// 判断如果参数不是一个对象,返回改参数
if(!isObject(source)) return source;
if(hash.has(source)) return hash.get(source); // 如何拷贝过该对象,则直接返回该对象
// 判断参数是对象还是数组来初始化返回值
let res = Array.isArray(source)?[]:{};
hash.set(source,res); // 哈希表添加新对象
// 循环参数对象的key
for(let key in source){
// 如果该key属于参数对象本身
if(Object.prototype.hasOwnProperty.call(source,key)){
// 如果该key的value值是对象,递归调用深拷贝方法进行拷贝
if(isObject(source[key])){
res[key] = deepCopy(source[key],hash);
}else{
// 如果该key的value值不是对象,则把参数对象key的value值赋给返回值的key
res[key] = source[key];
}
}
}
// 返回返回值
return res;
};
var obj3 = {
name:‘obj.name‘,
un:undefined,
nu:null,
sy:Symbol(123),
say:function(){
console.log(this.name);
},
reg:/d{6}/g,
date:new Date(),
child:{
name:‘child.name‘
}
}
var obj2=deepCopy(obj3)
console.log(obj2)

以上是关于手写深拷贝的主要内容,如果未能解决你的问题,请参考以下文章

js手写深拷贝的实现

js手写深拷贝的实现

JS手写面试题 --- 深拷贝

手写深拷贝

手写实现深拷贝函数

手写实现深拷贝函数