当前位置:网站首页>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()
边栏推荐
- JVM monitoring and diagnostic tools - command line
- Audio distortion analysis of DSP and DAC based on adau1452
- Test the foundation of development, and teach you to prepare for a fully functional web platform environment
- Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
- Tkinter window selects PCD file and displays point cloud (open3d)
- How can I check the DOI number of a foreign document?
- 软件测试的几个关键步骤,你需要知道
- 3428. Put apples
- Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
- C语言整理(待更新)
猜你喜欢
JMeter's own functions are not enough? Why don't you develop one yourself
线性代数(一)
LM小型可编程控制器软件(基于CoDeSys)笔记二十三:伺服电机运行(步进电机)相对坐标转换为绝对坐标
当我们谈论不可变基础设施时,我们在谈论什么
When we talk about immutable infrastructure, what are we talking about
You don't know the complete collection of recruitment slang of Internet companies
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
jmeter 函数助手 — — 随机值、随机字符串、 固定值随机提取
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
随机推荐
A very good JVM interview question article (74 questions and answers)
How to keep accounts of expenses in life
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
MySQL的安装
Several key steps of software testing, you need to know
Markdown displays pictures side by side
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
C language interview to write a function to find the first public string in two strings
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
693. Travel sequencing
Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
Subghz, lorawan, Nb IOT, Internet of things
UIC(组态UI工程)公版文件库新增7款行业素材
Jstack of JVM command: print thread snapshots in JVM
Ant manor safety helmet 7.8 ant manor answer
FlexRay通信协议概述
骑士战胜魔王(背包&dp)
How to solve sqlstate[hy000]: General error: 1364 field 'xxxxx' doesn't have a default value error
Jmeter自带函数不够用?不如自己动手开发一个
【GNN】图解GNN: A gentle introduction(含视频)