我想创建自定义成员装饰器,我的意思是:
class Example
{
@Decorator
test:any;
}
根据打字稿规范,这种装饰者的功能参数是:
the name of the nember - say key
the prototype of the class for an instance member - say targer
为什么原型传递给该功能?为什么不是原始类,而是基类?我想引用原始类,而不是基类。有没有什么方法可以引用原始类,也许是通过使用target
的构造函数属性,它是原始类的构造函数?
Decorator类将在运行时调用,所以也许我们可以使用这个事实,并使用一些明确的js?
请帮助我,我在这个问题上花了很多时间!
编辑: 让我们清楚一下我想达到什么效果。
class A {
attributes:string[];// Array to keep meta data about member
@Decorator("base")
test:string;
}
class B extends A {
@Decorator("varB")
my_var::string;
}
class C extends A {
@Decorator("varC")
my_var::string;
}
export function Attr(name: string) {
return function (target: any, key: any) {
if (!target.attributes) {
target.params = [];
}
target.attributes.push(name);
};
}
两个类B
和C
都继承了数组attributes
,但我B
中的数组与{ "base", "varB"}
和C
中的数组相同{ "base", "varC"}
。 base
来自基类装饰器。问题是真实的结果是{ "base", "varB", "varC"}
,因为函数装饰器中的target
始终是B
和C
的基类,Object
用于类A
}}。这总是原型。