Well, I am not sure if I describe the problem clearly, currently I am using ExtJS to do some developing, I saw some objects are "singleton", such as "Ext.Viewport", In C++, I can get the address of the object to see if they are actually same object, in Python, I can use "id" function to get the hash code of the object, and In Java I have similar built-in function "hashCode" to check if the objects are really same object, is there similar ways in javascript for this? so if there are some functions or operator in javascript, then I can tell if the object in ExtJS defined as "singleton" is really referencing to the same object.
-
You would need to iterate over the key/value pairs of each object that you're comparing to work out if they're the same. – Andy May 11 '15 at 12:46
5 Answers
You don't need anything so complicated. If two values are the same object, then they will be equal.
var foo = {};
var bar = foo;
alert(foo == bar);
If they are different (even if identical) objects, they won't be.
var foo = {};
var bar = {};
alert(foo == bar);

- 914,110
- 126
- 1,211
- 1,335
-
I am new to Javascript, when calling "var bar = foo", is bar a reference to foo or just a "deep copy" of foo? – python May 11 '15 at 12:48
-
1Neither. It's a shallow copy of `foo`. But the value of `foo` was a reference to the object in the first place. You never access objects by value in JS, only by reference. – Quentin May 11 '15 at 12:53
-
1@Quentin you're partly right but i wouldn't call that a "shallow copy". a "shallow copy" normally refers to a different object where all the keys have the same values, and any keys referring to objects refer to the same object in both copies. to describe this: `var foo = {}; var bar = foo;`, i would say that foo and bar reference the same object. – sleeparrow Feb 01 '18 at 20:54
Your question is not very clear, but I'll try to answer.
Javascript itself does not use unique identifiers for each object by default. You could add this if you wanted to.
Depending on your requirements, you could also use the typeof operator to compare the type.
ExtJs however, does use unique id's (either id or itemId) and also allows you to get the class name of the object that your using. So you could do this easily in ExtJs.
The answer depends on whether you are comparing the type of object, or the actual object instance itself.
This other SO answer may be beneficial

- 1
- 1

- 19,402
- 5
- 56
- 72
I think that I'm understanding you... but, did you check this JavaScript comparator ?
=== - equal value and equal type
!== - not equal value or not equal type
reference: Javascript comparators

- 770
- 14
- 20
If you want to know if two object references refer to the same single object instance, use ==
.
If you want to do a deep comparison of the referenced objects to see if the objects contain all the same values, even if they are not the same object instance, use Lodash's _.isEqual(a, b)
method.

- 8,186
- 2
- 45
- 40
could also convert both to json, and use something like jsonDiff: https://github.com/pkafel/json-diff

- 568
- 3
- 9