手记

【九月打卡】第13天 TypeScript学习 10-18章

课程名称: 晋级TypeScript高手,成为抢手的前端开发人才

课程章节: 10-18 【属性装饰器】属性装饰器应用,JS源码深剖

课程讲师: keviny79

课程内容:
属性装饰器:

// 声明 属性装饰器
function loginProperty(attrValue: any) {

  // targetclassPrototype 目标类原型
  // attrname 属性名,可以是字符串 或 symbol 类型
  return function (targetclassPrototype: object, attrname: string | symbol) {
    console.log("targetclassPrototype:", targetclassPrototype);
    console.log("attrname:", attrname);
    (targetclassPrototype.constructor as any).custLevelDescri = function () {
      console.log("消费5000元升级为贵宾");
      console.log("消费10000元升级为贵宾,赠送微波炉一个");
    }
  }
}

// 顾客目标类
class CustomerService {
  public custname: string = "王五"
  
  // 使用属性装饰器
  @loginProperty("顾客登记")
  public degree!: string
  
  constructor() {
  }

  show() {
    console.log("顾客名:", this.custname)
  }
}

(CustomerService as any).custLevelDescri()

属性装饰器接收两个参数

  • targetclassPrototype 目标类原型
  • attrname 属性名,可以是字符串 或 symbol 类型

课程收获:
属性装饰器的定义和使用

0人推荐
随时随地看视频
慕课网APP