当前位置: 首页 > news >正文

TS中的装饰器

环境:

  1. 安装:node
  2. 安装:typescript
  3. 初始化ts:tsc --init , 自动创建tsconfig.json文件
  4. 修改tsconfig.json中的配置生效:"experimentalDecorators": true,
  5. 安装ts-node:npm i ts-node -g,使用ts-node命令可以直接编译ts文件,不必再转为js
  6. 创建ts文件:index.ts
  7. 编译ts代码:ts-node index.js

1 类装饰器ClassDecorator

  • target是构造函数
// ClassDecorator的类型定义
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
// 类装饰器
const doc :ClassDecorator = (target: Function) => {console.log("target==>", target) // target==> [Function: MyClass]// 给类中添加一个属性nametarget.prototype.name = "小明"
}@doc 
class MyClass {constructor(){}
}const myClass:any = new MyClass()
console.log("name===>",myClass.name); //name===> 小明

2. 属性装饰器PropertyDecorator

// 类型定义
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
// 属性 装饰器
const doc :PropertyDecorator = (target: object,key: string|symbol) => {console.log("target===>",target,"key===>",key); //target===> {} key===> name
}class MyClass {@doc name:stringconstructor(){this.name = "小明"}
}

3. 方法装饰器MethodDecorator

declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
// 方法 装饰器
const doc: MethodDecorator = (target: object,propertyKey: string | symbol,descriptor: any
) => {console.log("target===>", target);console.log("propertyKey===>", propertyKey);console.log("descriptor===>", descriptor);
};class MyClass {constructor() {}@docgetName() {}
}
/*结果
target===> { getName: [Function (anonymous)] }
propertyKey===> getName	//方法的名字,只是个字符串
descriptor===> {value: [Function (anonymous)],writable: true,	// 可读enumerable: true,	// 可枚举configurable: true	// 可配置
}
*/

4. 参数装饰器ParameterDecorator

declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
// 参数 装饰器
const doc: ParameterDecorator = (target: object,propertyKey: string | symbol | undefined,parameterIndex: number
) => {console.log("target===>", target);console.log("propertyKey===>", propertyKey);console.log("parameterIndex===>", parameterIndex);
};class MyClass {constructor() {}getName(name: string, @doc age: number) {}
}
/*
target===> { getName: [Function (anonymous)] }
propertyKey===> getName
parameterIndex===> 1 // 参数索引位置,name是0,age是1
*/

5. 装饰器传参

// 这种写法可能比较好理解
/*
const log = (b: string) => {const doc: MethodDecorator = (target: any,propertyKey: string | symbol,descriptor: any) => {// getFun就是类中getName方法const getFun = descriptor.value;getFun(b);};return doc; // 在函数log中返回另一个函数doc
};
*/const log = (b: string) => {return (target: any, propertyKey: string | symbol, descriptor: any) => {// getFun就是类中getName方法// 通过descriptor.value获取装饰器装饰的方法const getFun = descriptor.value;getFun(b);};
};class MyClass {constructor() {}@log("我是装饰器上的字符串")getName(a: string) {console.log(a);  // 我是装饰器上的字符串}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Win电脑使用Ollama与Open Web UI搭建本地大语言模型运行工具
  • 第三届828 B2B企业节开幕,大腾智能携手华为云共谱数字化新篇章
  • 硬件工程师笔试面试知识器件篇——电感
  • springboot 的共享session方案?
  • 【AcWing】860. 染色法判定二分图
  • MySQL系列—8.存储结构
  • 中医世家龚洪海博士:用医术和真诚赢得患者的心
  • SPIRNGBOOT+VUE实现浏览器播放音频流并合成音频
  • 【开源大模型生态6】生态大咖与产品布局
  • 文本格式 .WAT
  • ueditor抓取图片
  • 2024.09.02 校招 实习 内推 面经
  • mysql快速定位cpu 占比过高的sql语句
  • UE5.3 新学到的一些性能测试合计(曼巴学习笔记)
  • 人工智能在行业中的应用
  • 【技术性】Search知识
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • C# 免费离线人脸识别 2.0 Demo
  • HTML中设置input等文本框为不可操作
  • iOS小技巧之UIImagePickerController实现头像选择
  • learning koa2.x
  • markdown编辑器简评
  • Promise初体验
  • Python学习之路16-使用API
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • vue:响应原理
  • 基于axios的vue插件,让http请求更简单
  • 排序算法学习笔记
  • 限制Java线程池运行线程以及等待线程数量的策略
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • $refs 、$nextTic、动态组件、name的使用
  • (10)ATF MMU转换表
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (初研) Sentence-embedding fine-tune notebook
  • (二)JAVA使用POI操作excel
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转)ORM
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core webapi 部署iis_一键部署VS插件:让.NET开发者更幸福
  • .Net Core 中间件验签
  • .net 微服务 服务保护 自动重试 Polly
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .NET开发不可不知、不可不用的辅助类(一)
  • .NET是什么
  • .NET值类型变量“活”在哪?
  • /var/spool/postfix/maildrop 下有大量文件
  • @EnableWebSecurity 注解的用途及适用场景
  • @FeignClient注解,fallback和fallbackFactory
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——
  • [Angular] 笔记 9:list/detail 页面以及@Output
  • [bug总结]: Feign调用GET请求找不到请求体实体类