当前位置:网站首页>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()
边栏推荐
- Open the blue screen after VMware installation
- CloudCompare-点对选取
- 拼多多败诉:“砍价免费拿”侵犯知情权但不构成欺诈,被判赔400元
- Implementation of VGA protocol based on FPGA
- JWT 认证
- 一段程序让你明白什么静态内部类,局部内部类,匿名内部类
- Redis (I) -- getting to know redis for the first time
- Apache ab 压力测试
- Redis(二)—Redis通用命令
- Common problems of caching in high concurrency scenarios
猜你喜欢
ICML 2022 | 探索语言模型的最佳架构和训练方法
Etcd database source code analysis -- starting from the start function of raftnode
安装VMmare时候提示hyper-v / device defender 侧通道安全性
jmeter 函数助手 — — 随机值、随机字符串、 固定值随机提取
VMware安装后打开就蓝屏
How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
Implementation of VGA protocol based on FPGA
CloudCompare-点对选取
进程间通信之共享内存
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
随机推荐
如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
Markdown 并排显示图片
string(讲解)
线性代数(一)
JVM monitoring and diagnostic tools - command line
3428. Put apples
Redis(二)—Redis通用命令
[FPGA] EEPROM based on I2C
Calculation model FPS
港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need
Three updates to build applications for different types of devices | 2022 i/o key review
Software testing knowledge reserve: how much do you know about the basic knowledge of "login security"?
Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens
基于ADAU1452的DSP及DAC音频失真分析
字符串常量与字符串对象分配内存时的区别
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
c语言(结构体)定义一个User结构体,含以下字段:
哈趣投影黑马之姿,仅用半年强势突围千元投影仪市场!
Oracle迁移中关于大容量表使用数据泵(expdp、impdp)导出导入容易出现的问题和注意事项
屏幕程序用串口无法调试情况