Zoltán Parragi
Zoltán Parragi

Reputation: 19

How references of the same object can't be equal?

Eg.:

let userOne = {
  name: "Test",
  surname: "Test"
}

let userTwo = {
  ...userOne
}
console.log(userOne === userTwo); // false

But, eg.:

console.log(userOne.name === userTwo.name); // true

So userOne and userTwo are two refeneces of the same object but console.log(userOne === userTwo); returns fasle.

Why is it?

Modification: Ok. In the previous example there are two object. But what about this:

let userOne = {
  name: "Test",
  surname: "Test surname",
  sizes: {
    width: 200,
    height: 200,
  }
}

let userTwo = {
  ...userOne
}
userTwo.sizes.width = 50;

alert(userOne.sizes.width); // 50

So userOne and userTwo are the references the same object.

But: alert(userOne == userTwo);// false

So the the two references does not point to the same object?

Upvotes: 0

Views: 101

Answers (4)

LC4-mafia
LC4-mafia

Reputation: 1

because of the logic you give in equals() method, by default they are equal.

Upvotes: 0

Thomas
Thomas

Reputation: 12657

Because thy are not references of the same object! They are two objects that (superficially) look the same.

Think any kind of store, you take a product from the shelf and there's a dozen identical products behind it. Just because they look the same doesn't make them the same item.

let userTwo = userOne; creates a second reference to the same item.

let userTwo = {...userOne} creates a new object and copies the properties that are own and enumerable to that new object.

In the previous example there are two object. But what about this:

let userOne = {
 name: "Test",
 surname: "Test surname",
 sizes: {
   width: 200,
   height: 200,
 }
}

let userTwo = {
 ...userOne
}
userTwo.sizes.width = 50;

alert(userOne.sizes.width); // 50

here userOne.size === userTwo.size. so if you change one ... the other one is the same one.

but userOne !== userTwo.

You and your sibling can have the same father, and if he loses an arm, well that doesn't only apply to one of you. But that doesn't make you and your sibling the same person.

Upvotes: 2

lyzaculous
lyzaculous

Reputation: 21

The es6 spread operator {...userOne}, creates a copy of the object userOne and store it in different memory location which is also equals to this,

let userTwo = Object.assign({}, userOne);

But if you assign the object to another,

let userThree = userOne; This is going to return true.

This on the other hand, only compares the values

userOne.name === userTwo.name so it is returning true.

Upvotes: 1

Quentin
Quentin

Reputation: 944083

They aren't the same object.

You have two different objects, but the second one has the properties of the first copied into it with spread syntax.

It would be clearer to demonstrate if more properties were included in the second object.

let userOne = {
  name: "Test",
  surname: "Test"
}

let userTwo = { ...userOne,
  yearOfBirth: 2000
};

console.log({
  userOne
});
console.log({
  userTwo
});

As you can see, the additional value is added to the second object but not the first.

Upvotes: 1

Related Questions