装饰器

  • 函数装饰器
MethodDecorator = <T>(target: Object, key: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | Void;
  • 属性装饰器
PropertyDecorator = (target: Object, key: string) => void;
  • class装饰器
ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction;

  

function funcDecorator(target, key, desc) {
    console.log('funcDecorator_target', target);
    console.log('funcDecorator_key', key);
    console.log('funcDecorator_desc', desc);
    return desc;
}

function propDecorator(target, key) {
    console.log('propDecorator_target', target);
    console.log('propDecorator_key', key);
}

function classDecorator(target) {
    console.log('classDecorator_target', target);
    return target;
}

@classDecorator
export class Test {
    @propDecorator
    public name: string = '';

    @funcDecorator
    public log(): void {}
}

  

posted @ 2021-08-21 15:14  671_MrSix  阅读(28)  评论(0编辑  收藏  举报