TypeScript / JavaScript - 如何获得 class 的 superclass?
TypeScript / JavaScript - How to get superclass of a class?
假设我有:
class Foo {}
class Bar extends Foo {}
var clazz = Bar;
我发现要得到 Bar
有 clazz.prototype.constructor
。
如何找出 Bar
的父级 class?
正如@MattiasBuelens 对答案的评论,它应该是:obj.constructor
而不是 obj.prototype.constructor
,因为 obj.prototype
为空(prototype
属性存在于 class Bar
但不存在于实例)。
至于获取 Foo
的构造函数,这是一个丑陋的技巧:
let FooCtor = Object.getPrototypeOf(Object.getPrototypeOf(obj)).constructor;
var foo = new FooCtor();
编辑
如果你想做同样的事情但使用 Bar
class 而不是它的实例,那么:
let FooCtor = Object.getPrototypeOf(Bar.prototype).constructor;
var foo = new FooCtor();
TypeScript 1.8 使用它来扩展 class(为了便于阅读,此处进行了缩减):
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));
它使用本地 Function
来实例化原型,并且隐藏了该闭包中两个 class 之间的关系。
感谢 Nitzan 的技巧,我只需要检查 class,而不是对象,所以我将其实例化以获取原型:
var clazz = TestPlanetModel;
var parent = Object.getPrototypeOf(Object.getPrototypeOf(new clazz())).constructor;
alert(parent === FrameModel);
没有实例化我没弄明白怎么办。
我最近发布了一个增强版的 TypeScript 编译器,让你在编码时和运行时都知道 类 和接口的所有反射元数据。以下代码适合您的需要:
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);
}
这是输出:
$ node main.js
MySuper instantiated with param: 3
superObj.id = 3
你可以找到我的项目here。
您可以使用“Object.getPrototypeOf (AnyClass)”
到 return AnyClass 的超级 class:
class Foo {}
class Bar extends Foo {};
let SuperClassOfBar = Object.getPrototypeOf (Bar);
if (SuperClassOfBar === Foo)
{ console.log
(`Superclass of Bar is Foo`); // yes it is
}
假设我有:
class Foo {}
class Bar extends Foo {}
var clazz = Bar;
我发现要得到 Bar
有 clazz.prototype.constructor
。
如何找出 Bar
的父级 class?
正如@MattiasBuelens 对答案的评论,它应该是:obj.constructor
而不是 obj.prototype.constructor
,因为 obj.prototype
为空(prototype
属性存在于 class Bar
但不存在于实例)。
至于获取 Foo
的构造函数,这是一个丑陋的技巧:
let FooCtor = Object.getPrototypeOf(Object.getPrototypeOf(obj)).constructor;
var foo = new FooCtor();
编辑
如果你想做同样的事情但使用 Bar
class 而不是它的实例,那么:
let FooCtor = Object.getPrototypeOf(Bar.prototype).constructor;
var foo = new FooCtor();
TypeScript 1.8 使用它来扩展 class(为了便于阅读,此处进行了缩减):
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));
它使用本地 Function
来实例化原型,并且隐藏了该闭包中两个 class 之间的关系。
感谢 Nitzan 的技巧,我只需要检查 class,而不是对象,所以我将其实例化以获取原型:
var clazz = TestPlanetModel;
var parent = Object.getPrototypeOf(Object.getPrototypeOf(new clazz())).constructor;
alert(parent === FrameModel);
没有实例化我没弄明白怎么办。
我最近发布了一个增强版的 TypeScript 编译器,让你在编码时和运行时都知道 类 和接口的所有反射元数据。以下代码适合您的需要:
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);
}
这是输出:
$ node main.js
MySuper instantiated with param: 3
superObj.id = 3
你可以找到我的项目here。
您可以使用“Object.getPrototypeOf (AnyClass)” 到 return AnyClass 的超级 class:
class Foo {}
class Bar extends Foo {};
let SuperClassOfBar = Object.getPrototypeOf (Bar);
if (SuperClassOfBar === Foo)
{ console.log
(`Superclass of Bar is Foo`); // yes it is
}