js的深拷贝

var obj1 = {
a: 1,
b: 2
}
var objString = JSON.stringify(obj1);
var obj2 = JSON.parse(objString);
obj2.a = 5;
console.log(obj1.a); // 1
console.log(obj2.a); // 5

export const deepcopy = function(source) {
if (!source) {
return source
}
let sourceCopy = source instanceof Arrary ? [ ] : { };
for ( let item in resource ) {
sourceCopy[item] = typeof source[item] === 'object' ? deepcopy (source[item]) : source[item];
}
return sourceCopy
}

你可能感兴趣的:(js的深拷贝)