当前位置:网站首页>小满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
}
}
边栏推荐
- AWS CodePipeLine deploys ECS across accounts
- [Free Column] Android Security for Peace Elite (FZ) APK Reverse Analysis
- [免费专栏] Android安全之Android工程模式
- Why is the data of maxcompute garbled when imported into mysql?The table of mysql is the encoding of udf8mb4
- 21天学习挑战赛--第四天打卡(横竖屏切换)
- 渗透测试——CFS三层靶机内网渗透实操
- Haven't tried line art videos this year??
- 使用.NET简单实现一个Redis的高性能克隆版(四、五)
- 典型的数据仓库模型实施过程详解
- 切绳子【洛谷P1577】【二分】
猜你喜欢
对应运放 RC 滤波负反馈的波形
Fully automated machine learning modeling!The effect hangs the primary alchemist!
《痞子衡嵌入式半月刊》 第 60 期
2022深圳(软考高级)信息系统项目管理师认证报名
ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
智驾科技完成C1轮融资,此前2轮已融4.5亿元
[] free column Android run Android, her - as command of safety
uniapp离线推送华为厂商申请流程
[免费专栏] Android安全之Android应用的汉化功能(修改so中的字符串内容)
Codesys结构变量编程应用(STRUCT类型)
随机推荐
hdu 2094 产生冠军(STL map || 拓扑 || STL set)
An overview of Office 365 Groups and how to create them
[Free Column] Android Security for Peace Elite (FZ) APK Reverse Analysis
新起之秀 DPU,正在掀起数据中心变革!
ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
2022.08.08_每日一题
MYSQL物理存储文件的页和INNOBUF的页是否有大小区别?
数学建模代码速成~赛前一个月~matlab~代码模板~吐血总结~三大模型代码(预测模型、优化模型、评价模型)
C#/VB.NET:从PowerPoint文档中提取文本和图片
shell之变量详解,让你秒懂!
Iptables防火墙常见的典型应用场景
Typora 结合 Picgo 自动上传图像
2022深圳(软考高级)信息系统项目管理师认证报名
[免费专栏] Android安全之安卓APK浅析
grafana docks local ldap
双屏协作更高效,华硕灵耀X 双屏Pro 2022创作体验再升级
环境:Flink版本:1.15.1jar包:flink-sql-connector-oracle
基于SSM实现手机销售商城系统
Paper sharing: "FED BN" uses the LOCAL BATCH NORMALIZATION method to solve the Non-iid problem
IDEA tools commonly used configuration