当前位置:网站首页>js装饰器@decorator学习笔记
js装饰器@decorator学习笔记
2022-07-07 02:06:00 【胖鹅68】
一、注解的基本用法
/**
* 注解执行是有顺序的, 顺序如下
* 1. 先执行带参数的注解
* 2. 再按照 出现顺序 执行 属性或 方法的注解
* 3. 最后按照 栈(先书写后执行) 类的注解
*/
@fn
@fn3
@fn2(10) // 这个可以理解为执行了 fn2(10)的函数
class MyClass {
@noEnumerable name = 'huangbiao' // name 无法被遍历到
@readOnly message = 'hello' // message 无法被修改
}
function fn(target) {
console.log('fn')
target.foo = 'bar'
}
function fn2(value) {
console.log('fn2')
return function (target) {
target.count = value
}
}
function fn3(target) {
console.log('fn3', target)
target.prototype.foo = 'baz'
}
function readOnly(target, name, descriptor) {
console.log('fn4')
console.log('target', target) // 目标类的.prototype
console.log('name', name) // 被修饰的类成员名称
/**
configurable: true // 默认值为true 当设置为false 则理解为该属性不可删除不可修改
enumerable: true // 默认值为true 当设置为false 则理解我改属性只读
initializer: ƒ ()
writable: true // 默认值为true 当设置为false 则理解为无法枚举 遍历属性时无法将该属性取出
*/
console.log('descriptor', descriptor) // 被修饰类成员的描述对象
descriptor.writable = false
}
function noEnumerable(target, name, descriptor) {
console.log('fn5')
console.log('target', target) // 目标类的.prototype
console.log('name', name) // 被修饰的类成员名称
/**
configurable: true // 默认值为true 当设置为false 则理解为该属性不可删除不可修改
enumerable: true // 默认值为true 当设置为false 则理解我改属性只读
initializer: ƒ ()
writable: true // 默认值为true 当设置为false 则理解为无法枚举 遍历属性时无法将该属性取出
*/
console.log('descriptor', descriptor) // 被修饰类成员的描述对象
descriptor.enumerable = false
}
console.log('MyClass.foo =>', MyClass.foo) // => bar
console.log('MyClass.count =>', MyClass.count) // => bar
console.log('new MyClass().foo => ', new MyClass().foo) // => baz
const obj = new MyClass()
// obj.message = 'world'
// console.log('obj.message => ', obj.message)
for (var key in obj) {
console.log('key, obj[key]', key, obj[key])
}
二、扩展类
function mixins(...list){
return function (target){
Object.assign(target.prototype, ...list)
}
}
const Foo = {
foo () {
console.log('foo func')
}
}
@mixins(Foo)
class MyClass {
}
let obj = new MyClass()
obj.foo()
边栏推荐
- Markdown 并排显示图片
- 港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need
- 博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
- ICML 2022 | 探索语言模型的最佳架构和训练方法
- 雷特智能家居龙海祁:从专业调光到全宅智能,20年专注成就专业
- JWT 认证
- You don't know the complete collection of recruitment slang of Internet companies
- string(讲解)
- 学习笔记|数据小白使用DataEase制作数据大屏
- [SOC FPGA] custom IP PWM breathing lamp
猜你喜欢
Rk3399 platform development series explanation (WiFi) 5.52. Introduction to WiFi framework composition
How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
How can I check the DOI number of a foreign document?
PostgreSQL database timescaledb function time_ bucket_ Gapfill() error resolution and license replacement
dolphinscheduler3.x本地启动
Three updates to build applications for different types of devices | 2022 i/o key review
JWT 认证
Common problems of caching in high concurrency scenarios
laravel 使用腾讯云 COS5全教程
可极大提升编程思想与能力的书有哪些?
随机推荐
请问如何查一篇外文文献的DOI号?
Several key steps of software testing, you need to know
CloudCompare-点对选取
安装VMmare时候提示hyper-v / device defender 侧通道安全性
Redis(一)——初识Redis
进程间通信之共享内存
Open the blue screen after VMware installation
How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
tkinter窗口选择pcd文件并显示点云(open3d)
UIC(组态UI工程)公版文件库新增7款行业素材
Experience sharing of contribution of "management world"
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
雷特智能家居龙海祁:从专业调光到全宅智能,20年专注成就专业
docker-compose启动redis集群
Crudini 配置文件编辑工具
Redis (II) - redis General Command
Swagger3 configuration
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
2022Android面试必备知识点,一文全面总结
直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级