0

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?

Thomas
  • 8,708
  • 1
  • 10
  • 21
  • 5
    Those are two different objects. Object comparison does not involve properties or property values. – Pointy Aug 09 '20 at 20:00
  • 2
    The simple fact they each have their own `{}` as value means they are not the same object reference at all and each are a new object. `{} !== {}` – charlietfl Aug 09 '20 at 20:02

4 Answers4

2

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.

Thomas
  • 8,708
  • 1
  • 10
  • 21
1

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.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

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.

lyzaculous
  • 11
  • 3
0

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

LC4-mafia
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/27102815) – thesis Sep 06 '20 at 17:12