类里面访问器的装饰器

本篇文章将介绍类中访问器的装饰器。

类的访问器指的是类中属性的getset 方法。


类访问器的装饰器和类方法的装饰器写法一样,通过descriptor可以拿到访问器的引用,从而可以对访问器属性进行一些修改。

function funcDescriptor(target: any, key: string, descriptor: PropertyDescriptor) {
  descriptor.writable = false
}

class Test3 {
  private _name: string
  constructor(name: string) {
    this._name = name
  }

  get name() {
    return this._name
  }

  @visitorDescriptor
  set name(name:string) {
    this._name = name
  }
}

const test4 = new Test3('jake')
test4.name = 'siple'
console.log(test4.name)

猜你喜欢

转载自blog.csdn.net/weixin_43800535/article/details/124465357