为什么两个相同的物体彼此不相同?

时间:2021-09-23 01:11:09

Seems like the following code should return a true, but it returns false.

看起来像下面的代码应该返回一个true,但它返回false。

var a = {};
var b = {};

console.log(a==b); //returns false
console.log(a===b); //returns false

How does this make sense?

这有什么意义?

7 个解决方案

#1


35  

The only difference between regular (==) and strict (===) equality is that the strict equality operator disables type conversion. Since you're already comparing two variables of the same type, the kind of equality operator you use doesn't matter.

常规(==)和严格(===)相等之间的唯一区别是严格相等运算符禁用类型转换。由于您已经在比较两个相同类型的变量,因此您使用的等式运算符的类型并不重要。

Regardless of whether you use regular or strict equality, object comparisons only evaluate to true if you compare the same exact object.

无论您使用常规还是严格相等,如果您比较同一个确切的对象,对象比较只会求值为true。

That is, given var a = {}, b = a, c = {};, a == a, a == b, but a != c.

也就是说,给定var a = {},b = a,c = {};,a == a,a == b,但是a!= c。

Two different objects (even if they both have zero or the same exact properties) will never compare equally. If you need to compare the equality of two object's properties, this question has very helpful answers.

两个不同的对象(即使它们都具有零或相同的确切属性)将永远不会相等。如果你需要比较两个对象属性的相等性,这个问题有非常有用的答案。

#2


12  

How does this make sense?

这有什么意义?

Because "equality" of object references, in terms of the == and === operators, is purely based on whether the references refer to the same object. This is clearly laid out in the abstract equality comparison algorithm (used by ==) and the strict equality comparison algorithm (used by ===).

因为对于==和===运算符,对象引用的“相等”纯粹基于引用是否引用相同的对象。这在抽象等式比较算法(由==使用)和严格相等比较算法(由===使用)中清楚地阐述。

In your code, when you say a==b or a===b, you're not comparing the objects, you're comparing the references in a and b to see if they refer to the same object. This is just how JavaScript is defined, and in line with how equality operators in many (but not all) other languages are defined (Java, C# [unless the operator is overridden, as it is for string], and C++ for instance).

在你的代码中,当你说a == b或a === b时,你没有比较对象,你要比较a和b中的引用,看它们是否引用同一个对象。这就是JavaScript的定义方式,并且与许多(但不是全部)其他语言中的相等运算符的定义一致(Java,C#[除非运算符被覆盖,因为它是用于字符串],例如C ++)。

JavaScript has no inbuilt concept of equivalence, a comparison between objects that indicates whether they're equivalent (e.g., have the same properties with the same values, like Java's Object#equals). You can define one within your own codebase, but there's nothing intrinsic that defines it.

JavaScript没有内置的等价概念,对象之间的比较表明它们是否相等(例如,具有相同属性的相同属性,如Java的Object#equals)。您可以在自己的代码库中定义一个,但没有任何内在的定义它。

#3


2  

===, the strictly equal operator for objects checks for identity.

===,对象的严格相等运算符检查标识。

Two objects are strictly equal if they refer to the same Object.

如果两个对象引用相同的Object,则它们严格相等。

Those are two different objects, so they differ.

这是两个不同的对象,所以它们不同。

Think of two empty pages of paper. Their attributes are the same, yet they are not the same thing. If you write something on one of them, the other wouldn't change.

想想两页空纸。它们的属性是相同的,但它们不是一回事。如果你在其中一个上写东西,另一个不会改变。

#4


2  

As from The Definitive Guide to Javascript.

从Javascript的权威指南。

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order.

对象不按值进行比较:即使两个对象具有相同的属性和值,它们也不相等。对于数组也是如此:即使它们具有相同顺序的相同值。

var o = {x:1}, p = {x:1};  // Two objects with the same properties
o === p                    // => false: distinct objects are never equal 
var a = [], b = [];        // Two distinct, empty arrays 
a === b                    // => false: distinct arrays are never equal 

Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types. Using this terminology, object values are references, and we say that objects are compared by reference: two object values are the same if and only if they refer to the same underlying object.

对象有时被称为引用类型,以将它们与JavaScript的原始类型区分开来。使用这个术语,对象值是引用,我们说通过引用比较对象:当且仅当它们引用相同的底层对象时,两个对象值是相同的。

var a = {};   // The variable a refers to an empty object. 
var b = a;    // Now b refers to the same object. 
b.property = 1;     // Mutate the object referred to by variable b. 
a.property          // => 1: the change is also visible through variable a. 
a === b       // => true: a and b refer to the same object, so they are equal. 

If we want to compare two distinct objects we must compare their properties.

如果我们想要比较两个不同的对象,我们必须比较它们的属性。

#5


1  

use JSON.stringify(objname);

var a = {name : "name1"};
var b = {name : "name1"};

var c = JSON.stringify(a);
var d = JSON.stringify(b);

c==d;
//true

#6


0  

How does this make sense?

这有什么意义?

Imagine these two objects:

想象一下这两个对象:

var a = { someVar: 5 }
var b = { another: 'hi' }

Now if you did a === b, you would intuitively think it should be false (which is correct). But do you think it is false because the objects contain different keys, or because they are different objects? Next imagine removing the keys from each object:

现在如果你做了=== b,你会直觉地认为它应该是假的(这是正确的)。但是你认为它是错误的,因为对象包含不同的键,或者因为它们是不同的对象?接下来想象从每个对象中删除键:

delete a.someVar
delete b.another

Both are now empty objects, but the equality check will still be exactly the same, because you are still comparing whether or not a and b are the same object (not whether they contain the same keys and values).

两者现在都是空对象,但是相等检查仍然完全相同,因为您仍然在比较a和b是否是同一个对象(而不是它们是否包含相同的键和值)。

#7


-1  

This is a workaround: Object.toJSON(obj1) == Object.toJSON(obj2)

这是一种解决方法:Object.toJSON(obj1)== Object.toJSON(obj2)

By converting to string, comprasion will basically be in strings

通过转换为字符串,comprasion将基本上是字符串

#1


35  

The only difference between regular (==) and strict (===) equality is that the strict equality operator disables type conversion. Since you're already comparing two variables of the same type, the kind of equality operator you use doesn't matter.

常规(==)和严格(===)相等之间的唯一区别是严格相等运算符禁用类型转换。由于您已经在比较两个相同类型的变量,因此您使用的等式运算符的类型并不重要。

Regardless of whether you use regular or strict equality, object comparisons only evaluate to true if you compare the same exact object.

无论您使用常规还是严格相等,如果您比较同一个确切的对象,对象比较只会求值为true。

That is, given var a = {}, b = a, c = {};, a == a, a == b, but a != c.

也就是说,给定var a = {},b = a,c = {};,a == a,a == b,但是a!= c。

Two different objects (even if they both have zero or the same exact properties) will never compare equally. If you need to compare the equality of two object's properties, this question has very helpful answers.

两个不同的对象(即使它们都具有零或相同的确切属性)将永远不会相等。如果你需要比较两个对象属性的相等性,这个问题有非常有用的答案。

#2


12  

How does this make sense?

这有什么意义?

Because "equality" of object references, in terms of the == and === operators, is purely based on whether the references refer to the same object. This is clearly laid out in the abstract equality comparison algorithm (used by ==) and the strict equality comparison algorithm (used by ===).

因为对于==和===运算符,对象引用的“相等”纯粹基于引用是否引用相同的对象。这在抽象等式比较算法(由==使用)和严格相等比较算法(由===使用)中清楚地阐述。

In your code, when you say a==b or a===b, you're not comparing the objects, you're comparing the references in a and b to see if they refer to the same object. This is just how JavaScript is defined, and in line with how equality operators in many (but not all) other languages are defined (Java, C# [unless the operator is overridden, as it is for string], and C++ for instance).

在你的代码中,当你说a == b或a === b时,你没有比较对象,你要比较a和b中的引用,看它们是否引用同一个对象。这就是JavaScript的定义方式,并且与许多(但不是全部)其他语言中的相等运算符的定义一致(Java,C#[除非运算符被覆盖,因为它是用于字符串],例如C ++)。

JavaScript has no inbuilt concept of equivalence, a comparison between objects that indicates whether they're equivalent (e.g., have the same properties with the same values, like Java's Object#equals). You can define one within your own codebase, but there's nothing intrinsic that defines it.

JavaScript没有内置的等价概念,对象之间的比较表明它们是否相等(例如,具有相同属性的相同属性,如Java的Object#equals)。您可以在自己的代码库中定义一个,但没有任何内在的定义它。

#3


2  

===, the strictly equal operator for objects checks for identity.

===,对象的严格相等运算符检查标识。

Two objects are strictly equal if they refer to the same Object.

如果两个对象引用相同的Object,则它们严格相等。

Those are two different objects, so they differ.

这是两个不同的对象,所以它们不同。

Think of two empty pages of paper. Their attributes are the same, yet they are not the same thing. If you write something on one of them, the other wouldn't change.

想想两页空纸。它们的属性是相同的,但它们不是一回事。如果你在其中一个上写东西,另一个不会改变。

#4


2  

As from The Definitive Guide to Javascript.

从Javascript的权威指南。

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order.

对象不按值进行比较:即使两个对象具有相同的属性和值,它们也不相等。对于数组也是如此:即使它们具有相同顺序的相同值。

var o = {x:1}, p = {x:1};  // Two objects with the same properties
o === p                    // => false: distinct objects are never equal 
var a = [], b = [];        // Two distinct, empty arrays 
a === b                    // => false: distinct arrays are never equal 

Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types. Using this terminology, object values are references, and we say that objects are compared by reference: two object values are the same if and only if they refer to the same underlying object.

对象有时被称为引用类型,以将它们与JavaScript的原始类型区分开来。使用这个术语,对象值是引用,我们说通过引用比较对象:当且仅当它们引用相同的底层对象时,两个对象值是相同的。

var a = {};   // The variable a refers to an empty object. 
var b = a;    // Now b refers to the same object. 
b.property = 1;     // Mutate the object referred to by variable b. 
a.property          // => 1: the change is also visible through variable a. 
a === b       // => true: a and b refer to the same object, so they are equal. 

If we want to compare two distinct objects we must compare their properties.

如果我们想要比较两个不同的对象,我们必须比较它们的属性。

#5


1  

use JSON.stringify(objname);

var a = {name : "name1"};
var b = {name : "name1"};

var c = JSON.stringify(a);
var d = JSON.stringify(b);

c==d;
//true

#6


0  

How does this make sense?

这有什么意义?

Imagine these two objects:

想象一下这两个对象:

var a = { someVar: 5 }
var b = { another: 'hi' }

Now if you did a === b, you would intuitively think it should be false (which is correct). But do you think it is false because the objects contain different keys, or because they are different objects? Next imagine removing the keys from each object:

现在如果你做了=== b,你会直觉地认为它应该是假的(这是正确的)。但是你认为它是错误的,因为对象包含不同的键,或者因为它们是不同的对象?接下来想象从每个对象中删除键:

delete a.someVar
delete b.another

Both are now empty objects, but the equality check will still be exactly the same, because you are still comparing whether or not a and b are the same object (not whether they contain the same keys and values).

两者现在都是空对象,但是相等检查仍然完全相同,因为您仍然在比较a和b是否是同一个对象(而不是它们是否包含相同的键和值)。

#7


-1  

This is a workaround: Object.toJSON(obj1) == Object.toJSON(obj2)

这是一种解决方法:Object.toJSON(obj1)== Object.toJSON(obj2)

By converting to string, comprasion will basically be in strings

通过转换为字符串,comprasion将基本上是字符串