简体   繁体   中英

How to get derived class name from base class?

Is is possible to get derived class name from base constructor?

class Entity {
    constructor() {
         // how to log here a class?
         console.log('a')
    }
}
class a extends Entity {}
new a()

You can output the name of a function in JavaScript/TypeScript using Function.name , see this answer :

 class Entity { constructor() { console.log(this.constructor.name) } } class A extends Entity {} const a = new A(); console.log(a.constructor.name); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM