Get list of extended classes in TypeScript -


i have:

classa  classb extends classa classc extends classb 

i created object on classc:

var myclassc= new classc(); 

classa has method:

public classname(): string {     var funcnameregex = /function (.{1,})\(/;     var results = (funcnameregex).exec(this["constructor"].tostring());     return (results && results.length > 1) ? results[1] : ""; } 

which returns "classc" string if call myclassc.getclassname();

what need list of classes base class: classc, classb, classa. there way this?

yeah, indeed possible:

function getclassname(obj: any): string {     var funcnameregex = /function (.{1,})\(/;     var results = (funcnameregex).exec(obj.constructor.tostring());     return (results && results.length > 1) ? results[1] : ""; }  class classa {     public classname(): string[] {         let classnames = [];         let obj = object.getprototypeof(this);         let classname: string;          while ((classname = getclassname(obj)) !== "object") {             classnames.push(classname);             obj = object.getprototypeof(obj);         }          return classnames;     } }  class classb extends classa {}  class classc extends classb {}  let = new classa(); console.log(a.classname); // ["classa"]  let b = new classb(); console.log(b.classname); // ["classb", "classa"]  let c = new classc(); console.log(c.classname); // ["classc", "classb", "classa"] 

(code in playground)


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -