5

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.

python
  • 1,762
  • 4
  • 21
  • 35
  • 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

3 Answers3

2

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);
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
  • 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
  • 1
    Neither. 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
  • @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
1

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

Community
  • 1
  • 1
Scriptable
  • 18,734
  • 5
  • 56
  • 68
1

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

mfruizs
  • 750
  • 13
  • 19