/**
 * 属性装饰器只能接收到两个参数
 * @param target Test 对应的 prototype
 * @param key  属性名字
 */
function nameDecorator(target: any, key: string):any {
  const descriptor: PropertyDescriptor = {
    writable: false
  }

  return descriptor;
}

class Test{ 
  @nameDecorator
  name = '1111'
}

const test = new Test();

// 本身是可以对属性进行修改的
// test.name = '2222';
// console.log(test.name); // 2222

// 在装饰器里面加以修饰就可以改变这个局面
test.name = '3333'
console.log(test.name); // 报错

 

相关文章:

  • 2021-07-02
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2022-01-09
猜你喜欢
  • 2022-01-28
  • 2022-12-23
  • 2021-09-18
  • 2021-06-13
  • 2021-08-08
  • 2022-12-23
  • 2022-01-18
相关资源
相似解决方案