Skip to content

装饰器模式之构造函数

JavaScript 中的装饰器模式常用于增强或修改类及其方法的功能。

JavaScript 中,装饰器模式可以通过高阶函数类装饰器等实现。

js
function Person ({ name, age } = {}) {
  this.name = name
  this.age = age
}

Person.prototype.getName = methodDecorator(function () {
  return this.name
})

function constructorDecorator (constructor) {
  return function (...args) {
    const instance = new constructor(...args)
    instance.msg = 'Hello World'
    return instance
  }
}

function methodDecorator (method) {
  return function (...args) {
    console.log('methodDecorator', this.name)
    return method(...args)
  }
}

const person = new constructorDecorator(Person)({
  name: 'Tom',
  age: 18
})

console.log(person)
person.getName()