如何判断两个javascript实例是否属于同一类类型? [英] how to tell if two javascript instances are of the same class type?

查看:343
本文介绍了如何判断两个javascript实例是否属于同一类类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 John Resig的简单继承类来定义一些类,比如说:

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

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

然后我有一些实例

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

如何确定 instanceA 是否属于与 instanceB 相同的类型?

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

注意:我不是要求检查它们是否都是 MyClass ,我需要确定一个类,然后查看另一个是否相同,无论它们是否是 MyClass s, MyOtherClass s或任何其他类型。

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.

谢谢

推荐答案

如果你需要知道它们是否是完全相同类的实例(不是共同祖先的子类等),那么这将起作用:

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

这篇关于如何判断两个javascript实例是否属于同一类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆