当前位置:网站首页>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()
边栏推荐
- Install mongodb database
- Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
- Markdown 并排显示图片
- postgresql 数据库 timescaledb 函数time_bucket_gapfill()报错解决及更换 license
- Qtthread, one of many methods of QT multithreading
- How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet
- 2022 Android interview essential knowledge points, a comprehensive summary
- 3531. Huffman tree
- Common problems of caching in high concurrency scenarios
- JWT 认证
猜你喜欢

VMware安装后打开就蓝屏

Common problems of caching in high concurrency scenarios

Open the blue screen after VMware installation

POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...

The difference between string constants and string objects when allocating memory

Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading

Experience sharing of contribution of "management world"

rt-thread 中对 hardfault 的处理

「解析」FocalLoss 解决数据不平衡问题

软件测试到了35岁,真的就干不动了吗?
随机推荐
c语言(结构体)定义一个User结构体,含以下字段:
How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet
C面试24. (指针)定义一个含有20个元素的double型数组a
JMeter's own functions are not enough? Why don't you develop one yourself
Database notes 04
What are the classic database questions in the interview?
Rk3399 platform development series explanation (interruption) 13.10, workqueue work queue
牛客小白月赛52 E.分组求对数和(二分&容斥)
693. Travel sequencing
2022 Android interview essential knowledge points, a comprehensive summary
Matlab / envi principal component analysis implementation and result analysis
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
进程间通信之共享内存
How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
"Parse" focalloss to solve the problem of data imbalance
Handling hardfault in RT thread
Ant manor safety helmet 7.8 ant manor answer
ST表预处理时的数组证明
直击2022ECDC萤石云开发者大会:携手千百行业加速智能升级
VMware安装后打开就蓝屏