Typescript with TypeLite - Run time type checking

£可爱£侵袭症+ 提交于 2019-12-04 10:37:45
Fenton

At runtime you are using plain JavaScript, so you could use this answer as it relates to plain JavaScript:

How do I get the name of an object's type in JavaScript?

Here is a TypeScript get-class-name implementation that can supply the name of the enclosing TypeScript class (the link also has a static separate version of this example).

class ShoutMyName {
    getName() { 
        var funcNameRegex = /function (.{1,})\(/;
        var anyThis = <any> this;
        var results = (funcNameRegex).exec(anyThis.constructor.toString());
        return (results && results.length > 1) ? results[1] : "";
    }
}

class Example extends ShoutMyName {
}

class AnotherClass extends ShoutMyName {
}

var x = new Example();
var y = new AnotherClass();

alert(x.getName());
alert(y.getName());

This doesn't give you data about the inheritance chain, just the class you are inspecting.

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