how to tell if two javascript instances are of the same class type?

爱⌒轻易说出口 提交于 2020-01-24 05:14:41

问题


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.

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/24959862/how-to-tell-if-two-javascript-instances-are-of-the-same-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!