当前位置:网站首页>JS decorator @decorator learning notes
JS decorator @decorator learning notes
2022-07-07 07:12:00 【Fat goose 68】
List of articles
One 、 Basic usage of annotations
/**
* Annotation execution is sequential , Order as follows
* 1. First execute the annotation with parameters
* 2. Again according to The order of appearance perform Attribute or Comment on method
* 3. Finally, according to Stack ( Write first and then execute ) Class annotation
*/
@fn
@fn3
@fn2(10) // This can be understood as execution fn2(10) Function of
class MyClass {
@noEnumerable name = 'huangbiao' // name Cannot be traversed to
@readOnly message = 'hello' // message Cannot be modified
}
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) // Target class .prototype
console.log('name', name) // The name of the decorated class member
/**
configurable: true // The default value is true When set to false It is understood that this attribute cannot be deleted or modified
enumerable: true // The default value is true When set to false Then understand that I change the attribute to read-only
initializer: ƒ ()
writable: true // The default value is true When set to false It is understood that enumeration cannot be performed When traversing a property, the property cannot be taken out
*/
console.log('descriptor', descriptor) // The description object of the decorated class member
descriptor.writable = false
}
function noEnumerable(target, name, descriptor) {
console.log('fn5')
console.log('target', target) // Target class .prototype
console.log('name', name) // The name of the decorated class member
/**
configurable: true // The default value is true When set to false It is understood that this attribute cannot be deleted or modified
enumerable: true // The default value is true When set to false Then understand that I change the attribute to read-only
initializer: ƒ ()
writable: true // The default value is true When set to false It is understood that enumeration cannot be performed When traversing a property, the property cannot be taken out
*/
console.log('descriptor', descriptor) // The description object of the decorated class member
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])
}
Two 、 The extension class
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()
边栏推荐
- 详解机器翻译任务中的BLEU
- Circulating tumor cells - here comes abnova's solution
- Databinding exception of kotlin
- Jetpack compose is much more than a UI framework~
- .net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
- leetcode 509. Fibonacci number
- Initial experience of addresssanitizer Technology
- 2022年全国所有A级景区数据(13604条)
- Basic introduction of JWT
- 联合索引ABC的几种索引利用情况
猜你喜欢
计算机服务中缺失MySQL服务
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
How does an enterprise manage data? Share the experience summary of four aspects of data governance
Tujia, muniao, meituan... Home stay summer war will start
2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
Unity3d learning notes
.net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
Non empty verification of collection in SQL
FPGA course: application scenario of jesd204b (dry goods sharing)
Academic report series (VI) - autonomous driving on the journey to full autonomy
随机推荐
Esxi attaching mobile (Mechanical) hard disk detailed tutorial
Algorithm --- bit count (kotlin)
How Oracle backs up indexes
ViewModelProvider. Of obsolete solution
栈题目:有效括号的嵌套深度
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Matlab tips (29) polynomial fitting plotfit
leetcode 509. Fibonacci number
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
Implementation of AVL tree
readonly 只读
请教一下,监听pgsql ,怎样可以监听多个schema和table
虚拟机的作用
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
MySQL view bin log and recover data
2022年全国所有A级景区数据(13604条)
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
工具类:对象转map 驼峰转下划线 下划线转驼峰
$parent(获取父组件) 和 $root(获取根组件)
计算机服务中缺失MySQL服务