Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:访问父类构造函数中子类的元数据_Javascript_Typescript_Decorator_Reflect - Fatal编程技术网

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

我想我可以访问
子类
构造函数中的元数据,并将其传递给
基类
的构造函数调用,但我有许多不同的子类,我希望避免为所有子类定义构造函数