通过该类的实例获取该类的名称

发布于 2024-08-17 10:11:26 字数 419 浏览 2 评论 0原文

我有一个函数,它接受(自定义)类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-08-24 10:11:26

您可以使用instanceof

   var a:Number;

   if (a instanceof Number)
   {
       trace("a is a number");
   }

You can use instanceof

   var a:Number;

   if (a instanceof Number)
   {
       trace("a is a number");
   }
我的奇迹 2024-08-24 10:11:26

获取实例类的另一种方法是使用,

var c:Class = instance["constructor"];

那么您可以执行以下操作:

switch(c)
{
    case Circle:
        whatever();
}

another way to get the class of an instance is using

var c:Class = instance["constructor"];

then you can do something like this:

switch(c)
{
    case Circle:
        whatever();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文