为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第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 类型

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

图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
9
获赞与收藏
5

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消