0

Possible Duplicate:
How do you determine equality for two JavaScript objects?

I want to check if two objects not different :

var v1 = {id:"llll", dd="kkkk"};
var v2 = {id:"llll", dd="kkkk"};

if (v1 == v2)
{
    alert("lll");
}

not work why ????

Community
  • 1
  • 1
user1194313
  • 141
  • 1
  • 2
  • 6

2 Answers2

5

Because objects are compared by reference:

  • Functions
  • Objects (Host objects, native objects/constructors and instances)
  • A common instance is {}, which is similar to new Object.

The following object types are compared by value, not by reference:

  • Strings
  • Numbers
  • Booleans
  • null and undefined

Additionally, there's one object which is never equal to itself, not even by reference:

var test = NaN;
alert ( test == NaN  ); // false
alert ( test == test ); // false (!)

To check whether two objects are equal, you have to define equality:

  • "Two objects are equal if they contain the same property names and values"
    Which implies that object A has to have the same number of properties as object B, and that every property in A has also to be a property of B.
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 3
    @user1194313, no, your question was "why doesn't this work?" which you didn't even take the time to write out correctly. Asking for code without putting any effort in is rude and unacceptable behavior. – zzzzBov Feb 11 '12 at 21:46
  • If you want code, there are plenty of **duplicate** questions. I posted this answer, because it seemed that you want to know *when* objects are equal. I have never seen anyone mentioning `NaN` in an answer to this question, so I wanted to create one. @user1194313 If you reduce this question to "Give meh teh codez to compare objects", I will vote to close this question as a duplicate (no offence intended). – Rob W Feb 11 '12 at 21:49
  • *"In JavaScript everything is an object."* Is that correct? What about JavaScript's primitive values? Quick test... `var s = 'string'; s.foo = "bar"; s.foo; // undefined` –  Feb 11 '12 at 21:50
  • Should be *"...the following **types** are compared by value"*, not *"object types"*. Also, `NaN` is not an object, but a Number value (despite its name `:)`). – Šime Vidas Feb 11 '12 at 21:51
  • @amnotiam No, of course. The values of the five primitive types are not objects. Only values of the Object type are objects. – Šime Vidas Feb 11 '12 at 21:52
  • @amnotiam I assume that you're pointing to the fact that, say, `5 instanceof Object` is false? The meaning of "object" in my answer is "a thing". If you find it confusing, I don't mind editing the answer. – Rob W Feb 11 '12 at 21:53
  • Just that I've heard *"In JavaScript everything is an object."* before, and I don't understand why people say that. Maybe I'm missing some fine detail? –  Feb 11 '12 at 21:57
  • 1
    @RobW I believe you mean the term "value". The term "object" is already defined in the spec to mean "a member of the Object type" - [see here](http://es5.github.com/#x4.3.3). A `5` is defined as a value (of the Number type). – Šime Vidas Feb 11 '12 at 22:01
  • 1
    @amnotiam By that they mean that arrays, functions, regular expressions, etc. are objects. I wouldn't use that definition, since it's ambiguous when placed out of context. – Šime Vidas Feb 11 '12 at 22:04
  • @amnotiam All primitive values in JavaScript have an Object representant, which define a set of methods. When you modify the `String` prototype, these methods are also available on `""`. However `"" instanceof String` is false, but `"".constructor === String` is true. Strictly, string primitives are not objects. However, my example shows that it's hard to say whether primitives are objects or not, in everyday language. – Rob W Feb 11 '12 at 22:09
  • 1
    I guess I'm just not sure it's helpful to call them objects even though JavaScript will convert them to their object type for you. Take this sentence *"The following object types are compared by value, not by reference: \*Strings..."* Given that, it would seem that the following should be true... `s1 = new String("foo"); s2 = new String("foo"); s1 == s2;` ...but it isn't. Just seems like the distinction is important. –  Feb 11 '12 at 22:25
0

Try using "===" instead of "==" to compare the objects.

DarkKnight
  • 37
  • 4
  • That fails to solve the issue. The issue is that the OP is comparing object _references_, not their actual values. – Kevin Ji Feb 12 '12 at 00:07
  • hmm..Oh. I get that now. so in order to compare the objects..comparision of each value or something like (JSON.stringify) on the objects can be done? – DarkKnight Feb 12 '12 at 19:48
  • What you need to do is call an object's internal `equals` function to equate the objects. The specifics of this method will vary between objects. For objects that you define, you should override the default `equals` and `compareTo` methods such that they would be applicable for your object. – Kevin Ji Feb 13 '12 at 03:10