-1

I know it sounds a bit stupid but I'm kinda stuck here.

let a={
    name:'Pete',
};
let b=a;

Change property of a.

a.name='Cindy';
b;  //{name: "Cindy"}, b follows a exactly.

Add a property for a.

a.age=20;
b; //{name: "Cindy", age: 20}, b still follows a.

Here's the problem, Now b doesn't follow a... it's now having a different value. WHY..?

a={};
b; // {name: "Cindy", age: 20}. b is different from a.
qqwenwenti
  • 63
  • 5
  • 3
    Because you are again initializing `a` as a empty object – brk Apr 08 '21 at 04:59
  • Object save the reference memory space, so `a.name = 'Cindy'` and `age = '20' saving in the same memory. but when you declare ` a = {}` its basically means that a make new address. so b won't follow suit to the new address unless you declare b = a again – Kerry Apr 08 '21 at 05:07

3 Answers3

3

Objects are stored by reference and not by value.

let a = { name:'Pete' };

The line above creates an object in memory and variable a stores a reference to this object.

let b = a

When you make b equal to a, the variable b also stores the reference to the same object.

Now, whenever you make any changes to the object, it will reflect in both the variables a and b, because they both store the reference to the same object.

enter image description here

Now, when you do a = {}, this creates a new object (empty) in memory and variable a now stores a reference to this new object. But this would not change variable b because it stores a reference to the first object which hasn't changed.

enter image description here

Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28
2

When you do

let a = {
  name:'Pete',
};
let b = a;
  • let a = { name:'Pete'} means that create allocate some memory and store that object {name: 'Pete'} in that. And that memory location can be accessed using a variable named a
  • let b = a, since a is an object doing b = a points the variable b to the same memory location.

So, now we have two variables pointing to the same memory location. Making changes on the properties using any variable will be reflected when we try to access the data using any of the other variables. That's the reason why when you do a.name='Cindy'; or adding a new prop a.age=20; will be seen when we try to access b as well.

             ___________________
            |                   |
a ----->    | Memory Location 1 |    <------ b
            | { name: 'Pete' }  |
            |___________________|

Coming to doing a={};, meaning you are asking to allocate a new memory. So, now a and b points to different memory locations while b pointing to the old memory location. Hence, we'll still be able to see the value as {name: "Cindy", age: 20}

             ___________________
            |                   |
a ----->    | Memory Location 2 |   
            |        {}         |
            |___________________|

             _____________________________
            |                             |
b ----->    | Memory Location 1           |   
            | { name: 'Cindy', age: 20 }  |
            |_____________________________|

Nithish
  • 5,393
  • 2
  • 9
  • 24
1

When you first created the object { name:'Pete' }, you assigned it to a, i.e. a is pointing at that object in memory.

You then assigned the a to b, but you are really assigning the object pointed to by a to b, so now b points to that same memory location which contains the same object.

By assigning the empty object literal to a, you have created a new object and a is now pointing to it.

The old object {name: "Cindy", age: 20} still exists, and b is still pointing to it. b does not change because a no longer points at the same object.

LevPewPew
  • 159
  • 7