14

I'm using John Resig's Simple Inheritance Class to define some classes, say:

var MyClass = Class.extend({});
var MyOtherClass = Class.extend({});

then I have some instances

var instanceA = new MyClass();
var instanceB = new MyClass();
var instancec = new MyOtherClass();

How can I determine if instanceA is of the same "type" as instanceB?

Note: I'm not asking to check if they are both a MyClass, I need to determine the class of one, and then see if the other is the same, regardless of whether they are MyClasss, MyOtherClasss or any other type.

halfer
  • 19,824
  • 17
  • 99
  • 186
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231

1 Answers1

24

If you need to know if they are instances of the exact same class, (not subclasses of a common ancestor, etc) then this will work:

instanceA.constructor === instanceB.constructor
Garry Shutler
  • 32,260
  • 12
  • 84
  • 119