为什么两个相同的物体彼此不相等? [英] Why are two identical objects not equal to each other?

查看:121
本文介绍了为什么两个相同的物体彼此不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎下面的代码应该返回一个true,但它返回false。

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

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

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

这有什么意义?

推荐答案

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

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.

无论您使用常规还是严格相等,对象比较仅评估为 true 如果您比较相同的确切对象

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

那是,给定 var a = {},b = a,c = {}; a == a a == b ,但 a!= c

That is, given var a = {}, b = a, c = {};, a == a, a == b, but 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.

这篇关于为什么两个相同的物体彼此不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆