This project is read-only.

Class inheritance chain

Topics: General
Nov 13, 2013 at 10:32 PM
With TypeScript's inheritance model is it possible, given either an instance of a class or the class constructor, to obtain a list of all constructor functions in the inheritance chain? For instance
class Organism
class Animal extends Organism
class Dog extends Animal
class Labradoodle extends Dog

var inst = new Labradoodle("T-Bone");
I can get the constructor with:
var dogCtor = inst.constructor;
(dogCtor === Dog) // true
Can I also get at the 'Animal' and 'Organism' constructor functions?

Nov 13, 2013 at 11:01 PM
Nevermind, I got it. Here's my solution if anyone else might want this. I think it's fairly 'standard':
        public static getPrototypeChain(ctor: any): any[]
            var chain = [];
            var proto = ctor.prototype;
            while (proto) {
                proto = Object.getPrototypeOf(proto);
            return chain;
Marked as answer by Barguast on 2/17/2014 at 11:28 AM