首页 > 解决方案 > 如何获取基类(Angular2+)中的派生类名?

问题描述

当我有一个扩展基类的类时,如何获取派生类的名称(在基类中)?

我在派生类调用的基类中有一个方法;我想知道哪个实例正在调用该方法;使用this.constructor.name我有当前的基类,而不是派生的。

角 4/5

谢谢。

[更新]

标签: angular

解决方案


您需要使用构造函数类的名称属性。

class Base {   
    public process(value: any) {
        alert(this.constructor["name"]); // alerts 'Derived' in this example
    }
}

class Derived extends Base {
    public process(value: any) {
        super.process(value); // use super to call methods on the base class
    }
}

(new Derived).process('someValue');

推荐阅读