-1

The object1 == object2 operation checks to see if the references are the same.

A lot of times we want to check if the objects structure (properties, values and even methods) are the same. We have to implement a isEqual() function for ourselves or use an external library.

Why isn't it just added to the javascript ECMA standard, like JSON.stringify() was?

Is there a specific reason?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Lombas
  • 1,000
  • 1
  • 8
  • 24
  • Have you tried the === (3 equal symbols) ? –  Oct 30 '15 at 19:36
  • 2
    @jeff `===` doesn't deep compare objets, AFAIK. – xlecoustillier Oct 30 '15 at 19:39
  • Looks very much like [this question](http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) -- which is a good and valid question. He's asking how he can, while you already know you can't and you're asking for the reasoning behind that. But the answer to his question addresses yours, I think. – 15ee8f99-57ff-4f92-890c-b56153 Oct 30 '15 at 20:01
  • So, what kind of answer do you expect here? Clearly nobody championed adding this to the ECMA standard with enough energy and convincing arguments to make it more important than other things that people were working on. – jfriend00 Oct 30 '15 at 20:01
  • 1
    Ask [TC39](https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md)? – Teemu Oct 30 '15 at 20:04
  • Why would you be comparing if methods are the same? That's a very odd use. To see what type of object it is, one would usually use `instanceof` or check the `constructor` property or key off some known property in the type of object. You can also check to see if the prototype is the exact same object. – jfriend00 Oct 30 '15 at 21:18
  • 1
    Questions about why something is not in a language do not really work well on Stack Overflow because there is usually not any specific answer that would be generally known or there's only a couple people in the world who might actually have an idea "why" and even they might just say "because nobody thought it was important enough to work on and prioritize"? It is quite possible there is no actual answer to this question. – jfriend00 Oct 30 '15 at 21:21
  • It seems that the comments already answer my question, because if it had a specific reason it would have been answered already. – Lombas Nov 04 '15 at 20:40

2 Answers2

0

For what I can gather, this hasn't been implemented because objects can have very different structures and only in very simple object structures consisting of name:value like obj = {name:"value",age="anotherValue"} the isEqual(obj1,obj2) would be useful.

Although I think it should be implemented nevertheless.

Lombas
  • 1,000
  • 1
  • 8
  • 24
0

Most probably because there is no obvious way to determine what exactly makes two objects equal.

For example, you could check that they have the same property names with the same values. However,

  • These values can be objects, should their equality be loosely checked recursively? Then, what should be done with cyclic references?
  • Should only enumerable properties be checked, or all of them?
  • Should only string properties be checked, or also symbols?
  • Should only properties be checked, or also internal slots?
  • If not all internal slots are checked, which ones? For example, should the [[Prototype]] values in ordinary objects be compared, or maybe call the [[GetPrototypeOf]] method? Should all function internal slots be compared, or otherwise how would you determine function equality?
  • Should only the property values be compared, or also the configurability, writability and enumerability?
  • What about accessor properties? Should getters be called and compare the returned values, or compare the getters and setters themselves?
  • What about proxy objects, which may return a different set of properties each time you ask them?

There is no best answer to these questions. For each person, object equality might mean different things. So they can write a function which checks exactly what they want.

Oriol
  • 274,082
  • 63
  • 437
  • 513