继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

暮雩
关注TA
已关注
手记 65
粉丝 9
获赞 5

课程名称: 晋级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 类型

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

图片描述

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP