简体   繁体   English

Javascript:为什么两个对象不相等?

[英]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. 我知道SO上有类似的问题,但没有一个能提供我想要的答案。

In JavaScript, if you do the following, the result will be false: 在JavaScript中,如果执行以下操作,结果将为false:

在此输入图像描述

I know it is to do with how JavaScript is defined in the spec, but why is it like that? 我知道这与规范中如何定义JavaScript有关,但为什么会这样呢? It is counter-intuitive. 这是违反直觉的。

If ("string" === "string") results in being true , then why doesn't ({ } === { }) result in being true? 如果("string" === "string")导致为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. 我在某处看到,等式算法被设计成类似于C ++或C#,但这就像发明了一种全新的引擎,它使用了十分之一的燃料而不是纯粹用于与其他汽车保持一致。

Why was JavaScript defined in this way? 为什么JavaScript以这种方式定义? 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. {}是在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. 它们是相同的,就像你有equals(obj1, obj2)方法来比较obj1和obj2的属性一样,如果两个对所有属性都有相同的值,它应该返回true。

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. 返回true,因为obj1和obj2指向同一个引用。

Finally, regarding string "abc" == "abc" , here == operator looks for actual string contents and returns true/false based on it. 最后,关于字符串"abc" == "abc" ,这里==运算符查找实际的字符串内容并基于它返回true / false。

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 . 另一种方法是,如果obj1 == obj2为真,那么当你执行obj1.x = 1 ,它也会改变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 , 因为javascript中的字符串即使是一个对象也被认为是一个原语,就像一个整数或一个布尔值,并且它们总是按值进行比较,因为这就是一个原语,一个简单的原子值..所以"string" == "string"1 = 1true == 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? 但是对象是复杂类型,它们是基元的集合,比较该聚合的规则,不能简化为标准,是{name:'a',address:'b',phone:'c'} != {name:'a',address:'b'}因为没有相同数量的属性? then in that case will {name:'x',address:'y'} == {name:'a',address'b'} ? 那么在这种情况下{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.. 它们具有相同的属性,但具有不同的值,或使其更复杂{name:'x',address'y'} == {address:'y',name:'c'} ..订单是否重要..

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.. 所以语言实现的比较至少是有用和可靠的..比较如果两个对象引用相等,这将允许进行验证,如obj == nullobj == this或者如果obja == objb那么我们至少可以知道我们在谈论什么对象/参考..

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' 所以总结原语总是会被它们的价值进行比较,而复杂的类型会通过它们的参考进行比较,这就是大多数语言所做的事情,而不是c ++的灵感或其他只是“游戏规则”的灵感

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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