js的深拷贝

2019-11-09  本文已影响0人  薄荷加冰

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
}

上一篇下一篇

猜你喜欢

热点阅读