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!

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Danmoreng
  • 2,367
  • 1
  • 19
  • 32
  • 1
    @aaronman: `===` is identical to `==` when the types are the same. –  May 23 '13 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 – David Mulder May 23 '13 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 – aaronman May 23 '13 at 20:49
  • @aaronman: I think the "2 hours" part of David's comment was intended to be directed at the OP. –  May 23 '13 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 – aaronman May 23 '13 at 20:54
  • @aaronman: I think he tagged you for the first part, but forgot to clarify for the second... either that or he thought you were the OP. And again, when the types are the same, the two operators behave identically. The `null` and `undefined` values are two different types. –  May 23 '13 at 20:56
  • @aaronman Oops, I got distracted halfway through and it became kinda messy. My bad and sorry for the confusion. – David Mulder May 23 '13 at 20:58
  • @squint, I know they are different types that's why I said your comment was correct, just clarifying motivations for using `===` – aaronman May 23 '13 at 20:59
  • @aaronman: Sorry, I misunderstood. –  May 23 '13 at 21:01

2 Answers2

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.

Joseph Myers
  • 6,434
  • 27
  • 36
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)) );
dandavis
  • 16,370
  • 5
  • 40
  • 36
  • There are too many weird little possibilities for incorrect results. I wouldn't recommend this. –  May 23 '13 at 20:54
  • @squint: i've had good luck with it... got an example of one or two of those for us? – dandavis May 23 '13 at 20:55
  • 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. –  May 23 '13 at 20:59
  • ...also, you're limited to a subset of data types that can be represented in the serialization. –  May 23 '13 at 21:01
  • i thought chrome cleaned up it's act on this, but i know there's no guarantee. if that's the only issue, then a .split("").sort() would work right? – dandavis May 23 '13 at 21:01
  • I'm not sure exactly what you mean by that. If you mean to split on each character, sort them and join them back into a string, then consider these two objects. `{"foo":"","bar":{}}`, `{"foo":{},"bar":""}` *Demo:* http://jsfiddle.net/ysngb/ –  May 23 '13 at 21:09
  • 1
    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. – dandavis May 23 '13 at 21:09