1

I have a simple question

let obj1 = {};
obj2= obj1;`

Here obj1 and obj2 are referencing the same object in the memory i.e. obj1===obj2 --> true. So by this logic any operation on obj2 will affect obj1.

Now if I assign a property to obj2

let obj1 = {};
obj2 = obj1;

obj2['a'] = {};
obj2 = obj2['a']; // why this operation didn't change obj1, if they are referencing the same object ?
console.log(obj1); // -- > { a: {} };
console.log(obj2); // -- > {};

now obj1===obj2 --> false why is this ?

Also How come obj2 = {} is different then obj2 = obj2['a'] ?

A screenshot for clarifying myself

enter image description here

Thanks for your help in advance.

niraj
  • 47
  • 7
  • 5
    `obj2 = obj2.a` reassigns `obj2` so it no longer points to `obj1`. – Aplet123 Dec 22 '20 at 01:55
  • Because when you do `obj2 = obj2[a]` you're reassigning the identifier name `obj2` to a completely new value, instead of just adding a property. – Andrew Li Dec 22 '20 at 01:56
  • for an object.. equality determines to where it points at.. to other things like strings or numbers.. it's just the values – The Bomb Squad Dec 22 '20 at 01:58
  • Thanks @Aplet123 That is the case when `obj2 = {}`. However, if we do `obj2['b'] = {}; obj2 = obj2['b']; ` this also modifies the `obj1` to `{'a':{'b':{}}}` . That part is confusing me. – niraj Dec 22 '20 at 02:01
  • `obj2.b = {}` doesn't reassign `obj2`, it assigns a value to a **property** of `obj2`, meaning it still points to `obj1`, meaning the changes will be reflected in `obj1`. The `obj2 = obj2.b` is irrelevant. – Aplet123 Dec 22 '20 at 02:05
  • 1
    @niraj Why does it confuse you? After `obj2.a = {}; obj2 = obj2.a;`, `obj1` points to the same object as it did before. `obj2` does not; it points to the `a` property of the object pointed to by `obj1`. `obj1 === obj2.a`. Then, if you add the property `b` to `obj2`, you simply nest another property in `obj2`. Since `obj1` still points to the “root” object, of course it reflects all the changes. – Sebastian Simon Dec 22 '20 at 02:08
  • 2
    @niraj Stepping through [this visualisation](http://pythontutor.com/visualize.html#code=let%20obj1%20%3D%20%7B%7D%3B%0Alet%20obj2%20%3D%20obj1%3B%0A%0Aobj2%5B'a'%5D%20%3D%20%7B%7D%3B%0Aobj2%20%3D%20obj2%5B'a'%5D%3B&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D&textReferences=false) might help clear some things up as well – Nick Parsons Dec 22 '20 at 02:09
  • You are confusing variable assignment with Object property assignment. – StackSlave Dec 22 '20 at 02:15
  • 1
    @NickParsons That's very helpful. Thanks a lot!! – niraj Dec 22 '20 at 02:19

2 Answers2

3

For objects, the assignment operator = assigns a reference to the object.

So in:

let obj1 = {};
let obj2 = obj1;

both obj1 and obj2 reference the same object. Now:

obj2['a'] = {};

creates a new property a and assigns it a value that is a reference to a new object. Since both obj1 and obj2 reference the same object, you'll also find:

obj2.a === obj1.a

But then:

obj2 = obj2['a']; // why this operation didn't change obj1, if they are referencing the same object ?

You've now assigned a different object to obj2, so it now references the new object initially assigned to obj2.a and:

obj1.a === obj2;

So obj1 was modified (or more correctly, the object referenced by obj1 was modified).

Some code:

// obj1 and obj2 reference the same object
let obj1 = {};
let obj2 = obj1;
console.log('obj2 === obj1     ' + (obj2 === obj1)); // true

// Assign new object to obj2.a
obj2['a'] = {};

// Affects obj1
console.log('obj2.a === obj1.a ' + (obj2.a === obj1.a)); // true

// Assign new object to obj2
obj2 = obj2['a'];

// obj2 now references a different object to obj1
console.log('obj1 === obj2     ' + (obj1 === obj2)); // false

// obj1.a still references new object
console.log('obj1.a === obj2   ' + (obj1.a === obj2)); // true
RobG
  • 142,382
  • 31
  • 172
  • 209
0

obj2 is an object, obj2[a] is another object. The result of comparing two objects is false.

 {} === {}    // false