admin管理员组

文章数量:1323380

Let's say I have:

class Foo {}
class Bar extends Foo {}
var clazz = Bar;

I figured out that to get Bar there's clazz.prototype.constructor.

How can I find out what is the parent class of Bar?

Let's say I have:

class Foo {}
class Bar extends Foo {}
var clazz = Bar;

I figured out that to get Bar there's clazz.prototype.constructor.

How can I find out what is the parent class of Bar?

Share Improve this question edited Aug 1, 2016 at 20:14 Ondra Žižka asked Aug 1, 2016 at 19:52 Ondra ŽižkaOndra Žižka 46.9k49 gold badges226 silver badges298 bronze badges 2
  • I think you meant obj.constructor === Bar, since obj.prototype === undefined – Mattias Buelens Commented Aug 1, 2016 at 19:57
  • Right. I've changed the question to focus on the classes. – Ondra Žižka Commented Aug 1, 2016 at 20:22
Add a ment  | 

4 Answers 4

Reset to default 5

As mented on the answer by @MattiasBuelens, it should be: obj.constructor and not obj.prototype.constructor as obj.prototype is null (the prototype property exists on the class Bar but not the instances).

As for getting the constructor of Foo, well this is an ugly hack:

let FooCtor = Object.getPrototypeOf(Object.getPrototypeOf(obj)).constructor;
var foo = new FooCtor();

Edit

If you want to do the same thing but with the Bar class instead of instance of it, then:

let FooCtor = Object.getPrototypeOf(Bar.prototype).constructor;
var foo = new FooCtor();

TypeScript 1.8 uses this to extend a class (reduced here for readability):

var __extends = function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = (__.prototype = b.prototype, new __());
};

var TestPlanetModel = (function (_super) {
    __extends(TestPlanetModel, _super);
    function TestPlanetModel() {
        _super.apply(this, arguments);
    }
    return TestPlanetModel;
}(FrameModel));

Which uses local Function to instantiate the prototype, and that hides the relation between the two classes in that closure.

Thanks to Nitzan for the trick, I only needed to check the class, not the object, so I instantiate it to get to the prototype:

var clazz = TestPlanetModel;
var parent = Object.getPrototypeOf(Object.getPrototypeOf(new clazz())).constructor;
alert(parent === FrameModel);

I didn't figure out how to do it without instantiating.

I recently released an enhanced version of the TypeScript piler that lets you know all the reflection metadata of classes and interfaces, both at coding time and runtime. The following code suits your needs:

class MySuper {
    id: number;
    constructor(n: number) {
        console.log("MySuper instantiated with param: " + n);
        this.id = n;
    }
}

class MySub extends MySuper {
    name: string;
}


let sub: Class = MySub.getClass();
if(sub.extends) {
    let superCtor = sub.extends.getConstructor<MySuper>(); //type param is optional, you can get "any" by default.
    //let's instantiate it!!!
    let superObj = new superCtor(3);
    console.log("superObj.id = " + superObj.id);
}

and this is the output:

$ node main.js
MySuper instantiated with param: 3
superObj.id = 3

You can find my project here.

You can use "Object.getPrototypeOf (AnyClass)" to return the super-class of AnyClass:

 class Foo {}
 class Bar extends Foo {};

 let SuperClassOfBar = Object.getPrototypeOf (Bar);
 if (SuperClassOfBar === Foo)
 { console.log 
    (`Superclass of Bar is Foo`);  // yes it is
 }
 

本文标签: reflectionTypeScriptJavaScriptHow to get superclass of a classStack Overflow