now know passing {} object is pass by reference, and if I change a property, this property will be changed everywhere else for the same object. so this what happen when I try this:
var someFunc = (val) => val.Hello = 'Hello World!';
var obj1 = {Hello:'Will Change'};
someFunc(obj1);
Console.Log(obj1.Hello); // output: Hello World!
Now I have a case where I want to update an object values without using constructor and deconstructor, because if I do so, I'll lose that reference.
So I can't use this:
var obj2 = { Hello: 'Another Hello', Other: 'This is another text' };
obj1 = {...obj1, ...obj2};
How can iterate through obj2 properties and assign them to obj1 without losing reference?
I did this solution in a different place. Still need to assign these values dynamically. I'm using typescript, so I can confirm that they at least have the same properties, would this help in any way?
Object.assign(obj1, obj2)
what they want: given a ={} b= {f:10} copy b to a by reference such that if later they do b.f = 800, it updates a.f also
Why not to use the same object?
I don't really care about b, I just need a to be updated when I consume b.
Обсуждают сегодня