Skip to content

装饰器工厂

装饰器工厂是一个返回装饰器函数的函数, 可以为装饰器添加参数, 可以更灵活的控制装饰器的行为

应用示例

需求: 定义一个LogInfo类装饰器工厂, 实现Person实例可以调用到introduce方法, 且introduce中输出内容的次数, 由LogInfo接收的参数决定

ts
type Constructor = new (...args: any[]) => {}

interface Person {
  introduce(): void
}

// 装饰器工厂, 接收一个参数 n, 返回一个类装饰器
function LogInfo(n: number) {
  // 返回的是装饰器
  return function (target: Constructor) {
    target.prototype.introduce = function() {
      for (let i = 0; i < n; i++) {
        console.log(`我是${this.name}, 今年${this.age}岁`)
      }
    }
  }
}

@LogInfo(3)
class Person {
  constructor(
    public name: string,
    public age: number
  ) {}
  speak() {
    console.log('Halo')
  }
}

const man = new Person('张三', 18)
man.introduce()