-1

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?

6
  • 2
    You didn't clone the object. user2 is a reference to the same object as user. Commented May 8, 2020 at 20:17
  • Do you know what exactly cloning is? Commented May 8, 2020 at 20:18
  • 1
    And assigning to a variable doesn't change other variables that contain references to the same object. Commented May 8, 2020 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. Commented May 8, 2020 at 20:21
  • @Barmar I didn't see that part Commented May 8, 2020 at 20:22

3 Answers 3

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.

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

4 Comments

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.
There's no copying when you assign an object. If you want to copy the object, use user2 = {...user}
You even said it in your question: Since they are pointing to the same memory addresses
Did not know about the obj = {...obj} copy behavior.
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).

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.