当前位置:网站首页>小满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
}
}
边栏推荐
猜你喜欢
![[免费专栏] Android安全之数据存储与数据安全【大集合】](/img/bc/9d71ff1de27156e11cde169022f4a1.png)
[免费专栏] Android安全之数据存储与数据安全【大集合】

新出现的去中心化科学能够为科学领域带来什么?

Fully automated machine learning modeling!The effect hangs the primary alchemist!

Open Source Summer | List Details Display Based on Ruoyi Architecture

ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
![[Free Column] Android Security for Peace Elite (FZ) APK Reverse Analysis](/img/22/a5129a310eec5ee1bf6f1cf90d05de.png)
[Free Column] Android Security for Peace Elite (FZ) APK Reverse Analysis

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

优秀的 Verilog/FPGA开源项目介绍(三十一)- OFDM

嵌入式开发:使用FILL提高代码完整性

鲜花线上销售管理系统的设计与实现
随机推荐
基于CC2530 E18-MS1-PCB Zigbee DIY作品(二)
Win10系统80端口被占用的解决方法
环境:Flink版本:1.15.1jar包:flink-sql-connector-oracle
AWS CodePipeLine 跨账号部署ECS
阿里云架构师耗时几个月编写这份MySQL高可用和性能优化技术宝典
数学建模代码速成~赛前一个月~matlab~代码模板~吐血总结~三大模型代码(预测模型、优化模型、评价模型)
【分享】入驻集简云开发者平台,如何使用Session Auth配置授权?
安装多版本php(php5.6,php7.2)
IDEA快捷代码实时模板
ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
宝塔面板安装使用
Openharmony Lightweight System Experiment--GPIO Lighting
Queue topic: Implementing stacks with queues
Flume (五) --------- 自定义 Interceptor、自定义 Source 与 自定义 Sink
Leetcode 739.每日温度 单调栈
laravel报错:TokenMismatchException in VerifyCsrfToken.php line 68:
基于CC2530 E18-MS1-PCB Zigbee DIY作品
shell之变量详解,让你秒懂!
AttributeError: module 'click' has no attribute 'get_os_args'
Iptables防火墙常见的典型应用场景