不会覆盖超类方法的装饰器

时间:2020-08-04 01:46:45

标签: javascript node.js typescript reflection decorator

我需要一个关于不覆盖超类原始方法的装饰器的示例

示例:

class Animal {

  sayHello(...params){
   }
}

@SomeDecorator()
class Dog extends Animal{

}

function SomeDecorator(newValue?){
   return (target) => {
      // method 
       const metadataValue = Reflect.getMetadata(
            'someKey',
            target.prototype.sayHello,
        ) || [];
        Reflect.defineMetadata(
            'someKey', 
            [ 
                ...metadataValue,
                {
                   newValue
                }
            ],
            target.prototype.sayHello,
        );
   }
}

我需要装饰器仅修改子类的元数据,而修改超类的方法

感谢您的帮助。

0 个答案:

没有答案
相关问题