简体   繁体   中英

Javascript: Why are two objects not equal?

I know there are similar questions to this on SO, but none of them provide the answer I am looking for.

In JavaScript, if you do the following, the result will be false:

在此输入图像描述

I know it is to do with how JavaScript is defined in the spec, but why is it like that? It is counter-intuitive.

If ("string" === "string") results in being true , then why doesn't ({ } === { }) result in being true?

I saw somewhere that the equality algorithm was designed to be similar to that of C++ or C#, but that's like inventing a brand new engine that uses 1/10th of the fuel and not using it purely for consistency with other cars.

Why was JavaScript defined in this way? Is there a reason behind this decision? Or was it just so it was seen doing the done thing?

{} is a literal to create object in javascript. That is you can replace

var obj = new Object();

with

var obj = {};

So any time you use {} you are creating a new object.

The line you mentioned, {} == {} creates two different objects and both has no properties. Identically they are same, like if you have equals(obj1, obj2) method which compares properties of both obj1 and obj2 and it should return true if both has same value for all properties.

But == operator will not check for properties. It checks if both the objects are pointing to same object/reference.

Whereas

var obj1 = {};
obj2 = obj1;
console.log(obj2 == obj1); //returns true

returns true because obj1 and obj2 are pointing to same reference.

Finally, regarding string "abc" == "abc" , here == operator looks for actual string contents and returns true/false based on it.

Strings are immutable, so two strings that have the same contents are functionally indistinguishable.

But objects and arrays can be modified, so just because they happen to have the same contents at some particular time doesn't mean they're the same. You can modify them and they'll then be different.

Another way to put this is that if obj1 == obj2 is true, then when you do obj1.x = 1 , it will also change obj2.x .

because a string in javascript even if being an object is considered a primitive, just like a integer or a boolean, and those are always compared by value, because that is what a primitive is, a simple atomic value.. so "string" == "string" , 1 = 1 and true == true ,

but objects are complex types, they are an aggregate of primitives, and the rules to compare that aggregate, cannot be simplified to standard, is {name:'a',address:'b',phone:'c'} != {name:'a',address:'b'} because does not have the same number of attributes? then in that case will {name:'x',address:'y'} == {name:'a',address'b'} ? they have the same attributes, but with different values, or to make it more complex {name:'x',address'y'} == {address:'y',name:'c'} .. does the order matter..

so if the comparison of complex objects can't be done in a simple standard way, it is better to leave to the programmer to implement the rule that applies to the situation..

so what comparison can the language implement that is at least helpful and reliable.. comparing if two object references are equal this will allow to make validations like obj == null or obj == this or if obja == objb so we can at least know of what object/reference are we talking about..

so in summary primitive will always be compared by their value, and complex types are compared by their reference, this is how most languages do, and is not by an inspirations of c++ or whatever is just 'the rules of the game'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM