0

I would like to know that since objects on JavaScript work as references, how I still to get the properties values from a cloned object as shown below:

let user = {
  name: 'Dan',
  age: 26,
  sayHi(){
      console.log('Hi', this.name);
  }
    
}

let user2 = user;

user.name = 'Suzan';
user = null;

console.log(user2.name);
//return Suzan

If object user received null, user2 shouldn't be null as well? Since they are pointing to the same memory addresses?

Ele
  • 33,468
  • 7
  • 37
  • 75
baum7
  • 3
  • 1
  • 2
    You didn't clone the object. `user2` is a reference to the same object as `user`. – Barmar May 08 '20 at 20:17
  • Do you know what exactly cloning is? – Ele May 08 '20 at 20:18
  • 1
    And assigning to a variable doesn't change other variables that contain references to the same object. – Barmar May 08 '20 at 20:18
  • @Ele The question isn't really about cloning. He wants to know why assigning `user = null;` doesn't cause `user2 = null` as well. – Barmar May 08 '20 at 20:21
  • @Barmar I didn't see that part – Ele May 08 '20 at 20:22
  • Once every object has been declared you can't replace it's memory area with another object or value. You can only declare variables (pointers) that points to object's memory location. Clearing memory from objects is GC (garbage collector) responsibility. When you assign another value to variable you just release reference to that object. When there is no variables (pointers) that points to this object, GC removes that object from memory. – Николай Гольцев May 08 '20 at 20:26

3 Answers3

1

After you do

user2 = user;

both variables are pointing to the same object. But when you do

user = null;

you just change where user is pointing, that has no effect on where user2 is pointing. It still points to the original object.

If you're familiar with languages like C, it's analogous to this:

struct u {
    char *name;
    int age;
};

struct u foo = {"Dan", 26};

struct u *user = &foo;
struct u *user2 = user;

user->name = "Suzan";
user = NULL;

printf("%s\n", user2->name);

Changing one pointer doesn't affect another pointer that points to the same memory.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks, so the values were reflected to user2 because it was the same object that user, right? I though that every value inside user will be "copied" by user2. – baum7 May 08 '20 at 20:48
  • 1
    There's no copying when you assign an object. If you want to copy the object, use `user2 = {...user}` – Barmar May 08 '20 at 20:54
  • You even said it in your question: **Since they are pointing to the same memory addresses** – Barmar May 08 '20 at 20:54
  • Did not know about the `obj = {...obj}` copy behavior. – Jared Farrish May 08 '20 at 21:32
1

Don't think about this in terms of memory addresses. JavaScript doesn't deal with computing at that level.

let user = {}

The value of user is a reference to an object.

let user2 = user;

Note the value of user2 is a reference to the same object.

user.name = 'Suzan';

Now the name property of that object has the value 'Suzan'.

This gives exactly the same result as using user2.name in the same way.

user = null;

Now the value of user is null.

The value of user2 is still a reference to that object.

Assigning null to user only changes the value of user. It doesn't affect the object that it's old value was a reference to.

console.log(user2.name);

Since user2 is a reference to the original object, this still works.

If you also assigned user2 = null then there would be no references to that object left and you would be unable to access it (and it would get garbage collected).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

When you make a reference of an object, the referenced object will only change the property not the shape of object(user2).

If you do change user object to null or empty array, user2 will always stay same.

Muhammad Lahin
  • 165
  • 1
  • 3
  • 12