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.

9
  • 5
    obj2 = obj2.a reassigns obj2 so it no longer points to obj1. Commented Dec 22, 2020 at 1: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. Commented Dec 22, 2020 at 1:56
  • 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. Commented Dec 22, 2020 at 2:08
  • 2
    @niraj Stepping through this visualisation might help clear some things up as well Commented Dec 22, 2020 at 2:09
  • 1
    @NickParsons That's very helpful. Thanks a lot!! Commented Dec 22, 2020 at 2:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

 {} === {}    // false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.