admin管理员组文章数量:1323557
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
?
-
I think you meant
obj.constructor === Bar
, sinceobj.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
4 Answers
Reset to default 5As 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
版权声明:本文标题:reflection - TypeScriptJavaScript - How to get superclass of a class? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742120816a2421684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论