Please explain this phenomenon to me.
this.testObject = { a: 1, b: { a: 3, b: { a: 78, b: null } } };
let g = this.testObject;
while (g.b) {
g = g.b;
}
g.b = {
a: 79,
b: null
};
console.log(this.testObject); // { a: 1, b: { a: 3, b: { a: 78, b: { a: 79, b: null } } } }
Now I decided to stick with this.testObject and removed g:
this.testObject = { a: 1, b: { a: 3, b: { a: 78, b: null } } };
while (this.testObject.b) {
this.testObject = this.testObject.b;
}
this.testObject.b = {
a: 79,
b: null
};
console.log(this.testObject); // {a": 78,b": {a": 79,b": null}
What’s going on? How is my assignment of g affecting this object this way?
In first snippet, if I did g=g.b then I am basically assigning a new value to g. However if I explicitly do this, then result isn't same?
this.testObject = {a:1, b:{a:3,b:{a:78, b:null}}};
let g = this.testObject;
while(g.b){
g = {a:7777, b:null};
}
g.b={a:79, b:null};
this.testObject; // {a": 1,b": {a": 3,b": {a": 78,b": null}