Javascript 类型脚本成员装饰器
我想创建自定义成员装饰器,我的意思是:Javascript 类型脚本成员装饰器,javascript,angularjs,typescript,Javascript,Angularjs,Typescript,我想创建自定义成员装饰器,我的意思是: class Example { @Decorator test:any; } 根据typescript规范,此类装饰器函数的参数为: the name of the nember - say key the prototype of the class for an instance member - say targer 为什么会有原型传递给该函数?为什么不是原始类,而是基类?我想提到的是原始类,而不是基类。是否有任何方法可以引用原始类,
class Example
{
@Decorator
test:any;
}
根据typescript规范,此类装饰器函数的参数为:
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
都继承了数组属性
,但我认为B
中的数组类似于{base”,“varB”}
和C
{code>{base”,“varC”}base
来自基类装饰器。问题是真正的结果是{“base”、“varB”、“varC”}
,因为函数装饰器中的target
始终是B
和C
的基类,而对象
则是A
类的基类。这始终是原型。如果你能与我们分享一下这位装饰师应该具备的能力,那会有所帮助。@toskv谢谢大家的关注,我编辑了这篇文章。