Created
November 30, 2020 14:32
-
-
Save 911992/d0fb9158a77192d73ee6236ef62a4ab2 to your computer and use it in GitHub Desktop.
javascript object deep copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function obj(arg_a,arg_b){ | |
var res={ | |
a: arg_a, | |
b: arg_b, | |
fn:function(){ | |
console.log("a: "+this.a + " , b: "+this.b); | |
} | |
}; | |
return res; | |
} | |
var _arr=[ | |
obj(0,1), | |
obj(2,3) | |
]; | |
var _arr_c=[]; | |
_arr.forEach(function(arg_obj){ | |
var _cpy={}; | |
Object.assign(_cpy,arg_obj); | |
_arr_c.push(_cpy); | |
}); | |
_arr_c[0].a=100 | |
_arr_c[0].b=101 | |
_arr_c[0].fn(); | |
_arr[0].fn(); | |
console.log(JSON.stringify(_arr)); | |
console.log(JSON.stringify(_arr_c)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment