JavaScript
x
5
1
var b = {};
2
var a = b;
3
b.test = 123;
4
console.log(a.test);
5
I am trying to write code similar to the above, however for sake of not having to describe context I’ll display that instead ^
After the line a = b
I want to lose the reference from a to b, so I can update b without it affecting a, and vice-versa
Is this possible?
Advertisement
Answer
You can clone your object with Object.assign()
:
JavaScript
1
2
1
var a = Object.assign({}, b);
2