Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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_Angularjs_Typescript - Fatal编程技术网

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谢谢大家的关注,我编辑了这篇文章。