当前位置:网站首页>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()
边栏推荐
- Party A's requirements for those who have lost 800 yuan
- Three updates to build applications for different types of devices | 2022 i/o key review
- 【解决】Final app status- UNDEFINED, exitCode- 16
- 高并发大流量秒杀方案思路
- VIM mapping large K
- Redis(二)—Redis通用命令
- Redis (I) -- getting to know redis for the first time
- UIC(组态UI工程)公版文件库新增7款行业素材
- 谷歌 Chrome 浏览器发布 103.0.5060.114 补丁修复 0-day 漏洞
- 2022 Android interview essential knowledge points, a comprehensive summary
猜你喜欢

面试中有哪些经典的数据库问题?

Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning

HKUST & MsrA new research: on image to image conversion, fine tuning is all you need

Experience sharing of contribution of "management world"

屏幕程序用串口无法调试情况

Jstack of JVM command: print thread snapshots in JVM

Implementation of VGA protocol based on FPGA

How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet

Several key steps of software testing, you need to know

【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
随机推荐
"Parse" focalloss to solve the problem of data imbalance
Markdown 并排显示图片
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
UIC (configuration UI Engineering) public file library adds 7 industry materials
Wechat applet hides the progress bar component of the video tag
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
CloudCompare-点对选取
[shell] summary of common shell commands and test judgment statements
线性代数(一)
The difference between string constants and string objects when allocating memory
LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
Array proof during st table preprocessing
屏幕程序用串口无法调试情况
FlexRay通信协议概述
Audio distortion analysis of DSP and DAC based on adau1452
Basic DOS commands
tkinter窗口选择pcd文件并显示点云(open3d)
高并发大流量秒杀方案思路
对称的二叉树【树的遍历】
Database notes 04