Javascript:访问父类构造函数中子类的元数据
我想在超级调用之前在父类的构造函数中访问子类的元数据 请参见以下示例:Javascript:访问父类构造函数中子类的元数据,javascript,typescript,decorator,reflect,Javascript,Typescript,Decorator,Reflect,我想在超级调用之前在父类的构造函数中访问子类的元数据 请参见以下示例: import "reflect-metadata"; const KEY = Symbol() function Decorator(name: string) { return function(target: any) { Reflect.defineMetadata(KEY, name, target); } } class BaseBase { constructor(na
import "reflect-metadata";
const KEY = Symbol()
function Decorator(name: string) {
return function(target: any) {
Reflect.defineMetadata(KEY, name, target);
}
}
class BaseBase {
constructor(name: string) {
console.log(name);
}
}
class Base extends BaseBase {
constructor() {
// prints "undefined" (because its defined on the Child constructor)
console.log(Reflect.getMetadata(KEY, Base));
console.log(Reflect.getMetadata(KEY, arguments.callee)); // ERROR: deprecated and removed in ES5
super("insert meta data here");
// will print "ChildName" but this can not be used before super call
console.log(Reflect.getMetadata(KEY, Reflect.getPrototypeOf(this).constructor));
console.log(Reflect.getMetadata(KEY, this.constructor));
}
}
@Decorator("ChildName")
class Child extends Base {}
new Child();
有没有办法将子类的元数据传递到BaseBase
类
我想我可以访问子类
构造函数中的元数据,并将其传递给基类
的构造函数调用,但我有许多不同的子类,我希望避免为所有子类定义构造函数