lundi 3 août 2020

Decorator that does not overwrite the superclass method

I need and example about a decorator that does not override the superclass original method

Example:

class Animal {

  sayHello(...params){
   }
}

@SomeDecorator()
class Dog extends Animal{

}

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

I need the decorator to only modify the metadata of the child class, but modify the method of the superclass

Thanks for your help.





Aucun commentaire:

Enregistrer un commentaire