0

I searched like 2 hours before asking this question and didn't find anything solving my problem although I think it's a rather basic one. In Java you just can use equal() to see if two objects have the same values. I thought this is how the == operator would work in Javascript. Appearently it does not. :(

I've been trying to compare two own created objects with the == operator, but it returns false although all values were equal. Why?

This is my function for creating the field object I use:

function field(player, figureKind) { this.player = player; this.figureKind = figureKind; this.hidden = true; if (player == 1 && hidden && figureKind != trapF && figureKind != flagF) { this.image = figureKind.getImage(0); } else if (player != 1 && hidden) { this.image = hidden; } else { this.image = figureKind.getImage(player); } this.setKind = setKind; function setKind(figureKind) { this.figureKind = figureKind; this.image = figureKind.getImage(player); } this.getKind = getKind; function getKind() { return this.figureKind; } this.getImage = getImage; function getImage() { return this.image; } this.getPlayer = getPlayer; function getPlayer() { return this.player; } this.removeHidden = removeHidden; function removeHidden() { this.hidden = false; this.image = figureKind.getImage(player); if (figureKind == trapF) this.image = figureKind.getImage(1); } } console.log(new field(2,flagF) == new field(2,flagF)); 

This returns false although the two objects should be the same, no?

If someone could tell me why this doesn't work AND how to make it work (cause I need this comparison for my game) I would be really thankful!

9
  • 1
    @aaronman: === is identical to == when the types are the same. Commented May 23, 2013 at 20:47
  • 1
    @aaronman: absolutely wrong, and how did you search for 2 hours when a single google search for "compare objects javascript" has the answer :O Commented May 23, 2013 at 20:47
  • I didn't search for 2hrs I thought for 2 seconds and posted an incorrect comment. I can't delete it LOL Commented May 23, 2013 at 20:49
  • @aaronman: I think the "2 hours" part of David's comment was intended to be directed at the OP. Commented May 23, 2013 at 20:52
  • @squint then why did he tag me, also your comment is right but if objects are null or undefined the two operators actually will have different results, so it is better to use === here Commented May 23, 2013 at 20:54

2 Answers 2

5

The objects are not the same in the mind of JavaScript even if they contain the same elements. Each separately-created object is a unique storage container, and so == will return false.

You will need to compare each property of the two objects if you want to see if they contain the same properties.

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

Comments

1

if you only need to compare data, and don't care about differences in methods, you can simply use JSON to deep-compare two identically-shaped objects:

console.log( JSON.stringify(new field(2,flagF)) == JSON.stringify(new field(2,flagF)) ); 

7 Comments

There are too many weird little possibilities for incorrect results. I wouldn't recommend this.
@squint: i've had good luck with it... got an example of one or two of those for us?
I'll describe first. It relies on the order of properties being consistent, and the specification makes no such guarantees. So you have the same potential for failure that you have when relying on for-in providing a predictable order of iteration.
...also, you're limited to a subset of data types that can be represented in the serialization.
DOH on the sorting, brain fart. you the man. user-land looping it is. good to know about the other issues with json. i think i can live with those limits, so i won't have to spend all weekend patching! thanks for the info squint.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.