TypeScript 类方法装饰器

type ClassMethodDecorator = (
  value: Function,
  context: {
    kind: 'method';
    name: string | symbol;
    static: boolean;
    private: boolean;
    access: { get: () => unknown };
    addInitializer(initializer: () => void): void;
  }
) => Function | void;

1、如果装饰器返回一个函数就会替代装饰的函数

function replaceMethod(
  originMethod: Function,
  content: ClassMethodDecoratorContext
) {
  return function (...args) {
    console.log(args);
    originMethod.call(this, args);
  };
}

2、装饰器可以传参数

@replaceMethod('7777')
myMethod

function replaceMethod(...args) {
  return function (
    originMethod: Function,
    content: ClassMethodDecoratorContext
  ) {
    return function (...hello) {
      originMethod.call(this,hello);
    };
  };
}
装饰器接参数重新类中的方法 停留时间执行
function Greeter(value: any, context: any) {}

function replaceMethod(num: number) {
  return function (
    originMethod: Function,
    content: ClassMethodDecoratorContext
  ) {
    return function (...hello) {
      setTimeout(() => {
        originMethod.call(this, hello);
      }, num);
    };
  };
}

@Greeter
class User {
  [propertyName: string]: any;
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  @replaceMethod(1000)
  hello(u) {
    console.log("元旦快乐" + " " + this.name + u);
  }
}

let u = new User("zhansan");
u.hello("0999");

3、方法装饰器的 { addInitializer } 相当于constructor 初始化对象 参数是一个函数,可以访问当前this

function initialInstance(
  originMethod: Function,
  content: ClassMethodDecoratorContext
) {
  content.addInitializer(function () {
    this[content.name] = this[content.name].bind(this);
  });
}
class Person {
  name: string;
  constructor(name: string) {
    this.name = name;

    // greet() 绑定 this
    // this.greet = this.greet.bind(this);
  }
  @initialInstance
  greet() {
    console.log(`Hello, my name is ${this.name}.`);
  }
}

4、一个方法可以有多个装饰器


/**
 *
 * @param originMethod @type {Function}
 * @param content  @type {ClassMethodDecoratorContext}
 */
function collectMethods(
  originMethod: Function,
  { addInitializer, name }: ClassMethodDecoratorContext
) {
  addInitializer(function () {
    let _this = this as Person;
    if (!_this.collectMethodSet) {
      _this.collectMethodSet = new Set();
    }
    _this.collectMethodSet.add(name);
  });
}
function initialInstance(
  originMethod: Function,
  content: ClassMethodDecoratorContext
) {
  content.addInitializer(function () {
    this[content.name] = this[content.name].bind(this);
  });
}
class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  [p: string]: any;

  @initialInstance
  @collectMethods
  greet() {
    console.log(`Hello, my name is ${this.name}.`);
  }
  @collectMethods
  handle() {}
}

const g = new Person("张三");
g.greet() //Hello, my name is 张三
console.log(g.collectMethodSet); //Set(2) { 'greet', 'handle' }

相关推荐

  1. TypeScript 方法装饰

    2023-12-31 01:26:04       36 阅读
  2. Python方法装饰

    2023-12-31 01:26:04       13 阅读
  3. typescript 命名空间、装饰

    2023-12-31 01:26:04       10 阅读
  4. python装饰基础

    2023-12-31 01:26:04       36 阅读
  5. python装饰编写单体

    2023-12-31 01:26:04       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-31 01:26:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-31 01:26:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-31 01:26:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-31 01:26:04       20 阅读

热门阅读

  1. 什么是redis雪崩

    2023-12-31 01:26:04       33 阅读
  2. ELF Strip

    2023-12-31 01:26:04       27 阅读
  3. RabbitMQ消息队列常见面试题

    2023-12-31 01:26:04       36 阅读
  4. Git 命令

    2023-12-31 01:26:04       28 阅读
  5. 【Linux】修复 Linux 错误 - 地址已在使用中

    2023-12-31 01:26:04       33 阅读
  6. ❀My排序算法学习之选择排序❀

    2023-12-31 01:26:04       35 阅读
  7. CAPL解析DBC文件

    2023-12-31 01:26:04       51 阅读
  8. OpenFeign相关面试题及答案

    2023-12-31 01:26:04       27 阅读
  9. linux安装nginx

    2023-12-31 01:26:04       28 阅读
  10. asset模块在Github的含义

    2023-12-31 01:26:04       32 阅读
  11. 离线服务器中python包的安装

    2023-12-31 01:26:04       34 阅读
  12. webscoket多个客户端同时向一个服务器传输数据

    2023-12-31 01:26:04       35 阅读
  13. PHP8使用PDO对象增删改查MySql数据库

    2023-12-31 01:26:04       35 阅读