似乎下面的代码应该返回一个true,但它返回false。
var a = {};
var b = {};
console.log(a==b); //returns false
console.log(a===b); //returns false
这有什么意义?
答案 0 :(得分:36)
常规(==
)和严格(===
)相等之间的唯一区别是严格相等运算符禁用类型转换。由于您已经在比较两个相同类型的变量,因此您使用的等式运算符的类型并不重要。
无论您使用常规还是严格相等,如果您比较相同的确切对象,对象比较仅会评估为true
。
即给定var a = {}, b = a, c = {};
,a == a
,a == b
,但a != c
。
两个不同的对象(即使它们都具有零或相同的确切属性)永远不会相同。如果需要比较两个对象属性的相等性,this question has very helpful answers。
答案 1 :(得分:13)
这有什么意义?
因为对于==
和===
运算符,对象引用的“相等”是纯粹,具体取决于引用是否引用相同< / strong>对象。这显然在the abstract equality comparison algorithm(由==
使用)和strict equality comparison algorithm(由===
使用)中列出。
在您的代码中,当您说a==b
或a===b
时,您没有比较对象,您正在比较a
中的引用, b
看看他们是否引用同一个对象。这就是如何定义JavaScript,以及如何定义许多(但不是全部)其他语言中的相等运算符(Java,C#[除非运算符被覆盖,如it is for string
]和C ++)
JavaScript没有等价的内置概念,对象之间的比较表明它们是否相等(例如,具有相同属性的相同属性,如Java的Object#equals
)。您可以在自己的代码库中定义一个,但没有任何内在的定义它。
答案 2 :(得分:2)
===
,对象的strictly equal运算符检查身份。
如果两个对象引用相同的对象,则它们严格相等。
这是两个不同的对象,因此它们不同。
想想两页空纸。它们的属性是相同的,但它们不是一回事。如果你在其中一个上写东西,另一个就不会改变。
答案 3 :(得分:2)
从Javascript的权威指南。
对象不按值进行比较:即使两个对象具有相同的属性和值,它们也不相等。对于数组也是如此:即使它们具有相同顺序的相同值。
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
对象有时被称为引用类型,以区别于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.
如果我们想要比较两个不同的对象,我们必须比较它们的属性。
答案 4 :(得分: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
答案 5 :(得分:0)
这有什么意义?
想象一下这两个对象:
var a = { someVar: 5 }
var b = { another: 'hi' }
现在如果你做a === b
,你会直觉地认为它应该是假的(这是正确的)。但是你认为它是错误的,因为对象包含不同的键,或者因为它们是不同的对象?接下来想象从每个对象中删除键:
delete a.someVar
delete b.another
两者现在都是空对象,但是相等检查仍然完全相同,因为您仍然在比较a
和b
是否是同一个对象(而不是它们是否包含相同的对象)键和值)。
答案 6 :(得分:0)
{} === {}
在JavaScript中返回false
的简要说明:从MDN Web文档-Working with objects: Comparing objects
。
在JavaScript中,对象是引用类型。即使两个不同的对象具有相同的属性,它们也永远不会相等。仅将同一对象引用与其自身进行比较会得出true。
// Two variables, two distinct objects with the same properties
var fruit = {name: 'apple'};
var fruitbear = {name: 'apple'};
fruit == fruitbear; // return false
fruit === fruitbear; // return false
// Two variables, a single object
var fruit = {name: 'apple'};
var fruitbear = fruit; // Assign fruit object reference to fruitbear
// Here fruit and fruitbear are pointing to same object
fruit == fruitbear; // return true
fruit === fruitbear; // return true
fruit.name = 'grape';
console.log(fruitbear); // output: { name: "grape" }, instead of { name: "apple" }
有关比较运算符的更多信息,请参见Comparison operators。
答案 7 :(得分:0)
In Javascript each object is unique hence `{} == {}` or `{} === {}` returns false. In other words Javascript compares objects by identity, not by value.
1. Double equal to `( == )` Ex: `'1' == 1` returns true because type is excluded
2. Triple equal to `( === )` Ex: `'1' === 1` returns false compares strictly, checks for type even
答案 8 :(得分:-1)
这是一种解决方法:Object.toJSON(obj1) == Object.toJSON(obj2)
通过转换为字符串,comprasion将基本上是字符串