Skip to content
Advertisement

Remove reference to another object in javascript

var b = {};
var a = b;
b.test = 123;
console.log(a.test);

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():

var a = Object.assign({}, b);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement