通过该类的实例获取该类的名称
我有一个函数,它接受(自定义)类的 2 个实例作为参数。但它们都可以是多个类之一,然后我需要根据它们的类型调用另一个函数。我想做这样的事情:
function any_any(inst1, inst2) {
this[inst1.classname + "_" + inst2.classname] (inst1, inst2);
}
function Circle_Line(circle:Circle, line:Line) {
//treat this case
}
我应该在每个类中定义“类名”,还是有更好的方法来获取实例的类名? 我不知道如何让 typeof() 返回自定义类的“object”以外的任何内容,也许有可能吗?
编辑:使用instanceof运算符会很不方便,因为每个类可以是6个中的1个(当前)。
I have a function that takes as parameters 2 instances of a (custom) class. But they can each be one of several classes, and I need to then call another function based on what type they are. I'd like to do something like this:
function any_any(inst1, inst2) {
this[inst1.classname + "_" + inst2.classname] (inst1, inst2);
}
function Circle_Line(circle:Circle, line:Line) {
//treat this case
}
Should I go and define 'classname' in each of my classes, or is there a better way to get the class name of an instance?
I don't know how to get typeof() to return anything other than 'object' for a custom class, maybe it's possible?
EDIT: It would be inconvenient to use the instanceof operator, as each class can be 1 of 6 (currently).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 instanceof 或 “is”运算符,或 getQualifiedClassName 方法
You can use instanceof, or the 'is' operator, or the getQualifiedClassName method
您可以使用
instanceof
You can use
instanceof
获取实例类的另一种方法是使用,
那么您可以执行以下操作:
another way to get the class of an instance is using
then you can do something like this: