当前位置:网站首页>小满nestjs(第三章 前置知识装饰器)
小满nestjs(第三章 前置知识装饰器)
2022-08-09 18:40:00 【小满zs】
1、什么是装饰器
装饰器是一种特殊的类型声明,他可以附加在类,方法,属性,参数上面
装饰器写法 tips(需要开启一项配置)
类装饰器 主要是通过@符号添加装饰器
他会自动把class的构造函数传入到装饰器的第一个参数 target
然后通过prototype可以自定义添加属性和方法
function decotators (target:any) {
target.prototype.name = '小满'
}
@decotators
class Xiaoman {
constructor () {
}
}
const xiaoman:any = new Xiaoman()
console.log(xiaoman.name)
属性装饰器
同样使用@符号给属性添加装饰器
他会返回两个参数
1.原形对象
2.属性的名称
const currency: PropertyDecorator = (target: any, key: string | symbol) => {
console.log(target, key)
}
class Xiaoman {
@currency
public name: string
constructor() {
this.name = ''
}
getName() {
return this.name
}
}
参数装饰器
同样使用@符号给属性添加装饰器
他会返回两个参数
1.原形对象
2.方法的名称
3.参数的位置从0开始
const currency: ParameterDecorator = (target: any, key: string | symbol,index:number) => {
console.log(target, key,index)
}
class Xiaoman {
public name: string
constructor() {
this.name = ''
}
getName(name:string,@currency age:number) {
return this.name
}
}
方法装饰器
同样使用@符号给属性添加装饰器
他会返回两个参数
1.原形对象
2.方法的名称
3.属性描述符 可写对应writable,可枚举对应enumerable,可配置对应configurable
const currency: MethodDecorator = (target: any, key: string | symbol,descriptor:any) => {
console.log(target, key,descriptor)
}
class Xiaoman {
public name: string
constructor() {
this.name = ''
}
@currency
getName(name:string,age:number) {
return this.name
}
}
边栏推荐
- C#/VB.NET:从PowerPoint文档中提取文本和图片
- winpe工具WEPE微PE工具箱
- Detailed explanation of VIT transformer
- [免费专栏] Android安全之安卓APK浅析
- php安装make出现“collect2:error:ldreturned1exitstatus
- From functional testing to automated testing, do you know their shortcomings?
- 基于CC2530 E18-MS1-PCB Zigbee DIY作品(三)
- mysql 重复数据 分组 多条最新的记录
- 安装多版本php(php5.6,php7.2)
- 【分享】入驻集简云开发者平台,如何使用Session Auth配置授权?
猜你喜欢
启动 CM agent 报错——ImportError: libssl.so.10: cannot open shared object file: No such file or directory
[] free column Android dynamic debugging GDB APP of safety
IDEA快捷代码实时模板
2022.08.05_每日一题
golang单元测试:testing包的基本使用
2021 RoboCom 世界机器人开发者大赛-本科组(决赛)
IDEA tools commonly used configuration
pytest框架之mark标记功能详细介绍
鲜花线上销售管理系统的设计与实现
C#/VB.NET:从PowerPoint文档中提取文本和图片
随机推荐
[免费专栏] Android安全之GDB动态调试APP
[免费专栏] Android安全之动态代码注入技术(利用JDB调试APK)
[免费专栏] Android安全之安卓APK浅析
[免费专栏] Android安全之Root检测和绕过(浅析)
mysql 重复数据 分组 多条最新的记录
Haven't tried line art videos this year??
Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE
有文章说明或者证明MYSQL 嵌套子查询不足之处吗?
[] free column Android run Android, her - as command of safety
Laravel DB批量更新的方法
以技术创新加速国家“碳中和”建设进程,华为云创新中心助力欣冠精密实现云智控“气”
poj 1182 食物链(带权并查集)
Samsung's flagship discount is 1,800, Apple's discount is over 1,000, and the domestic flagship is only reduced by 500 to send beggars
RT-Thread推荐入围国赛及群体挑战赛名单
单调栈
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
[免费专栏] Android安全之Android奇淫run-as命令
开源一夏 | 基于若依架构的列表详情展示
Paper sharing: "FED BN" uses the LOCAL BATCH NORMALIZATION method to solve the Non-iid problem
安装多版本php(php5.6,php7.2)