当前位置:网站首页>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()
边栏推荐
猜你喜欢

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

LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates

Redis(二)—Redis通用命令

How can I check the DOI number of a foreign document?

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

【GNN】图解GNN: A gentle introduction(含视频)

字符串常量与字符串对象分配内存时的区别

Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】

Markdown displays pictures side by side

Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
随机推荐
C interview 24 (pointer) define a double array with 20 elements a
Jstack of JVM command: print thread snapshots in JVM
基于ADAU1452的DSP及DAC音频失真分析
Subghz, lorawan, Nb IOT, Internet of things
【解决】Final app status- UNDEFINED, exitCode- 16
360 Zhiyu released 7.0 new products to create an exclusive "unified digital workspace" for the party, government and army, and central and state-owned enterprises
[shell] summary of common shell commands and test judgment statements
Laravel uses Tencent cloud cos5 full tutorial
谷歌 Chrome 浏览器发布 103.0.5060.114 补丁修复 0-day 漏洞
3531. Huffman tree
ICML 2022 | 探索语言模型的最佳架构和训练方法
"Parse" focalloss to solve the problem of data imbalance
基本Dos命令
PostgreSQL database timescaledb function time_ bucket_ Gapfill() error resolution and license replacement
雷特智能家居龙海祁:从专业调光到全宅智能,20年专注成就专业
A very good JVM interview question article (74 questions and answers)
How to keep accounts of expenses in life
Swagger3 configuration
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
港科大&MSRA新研究:关于图像到图像转换,Fine-tuning is all you need