当前位置:网站首页>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()
边栏推荐
- 关于二进制无法精确表示小数
- Data of all class a scenic spots in China in 2022 (13604)
- Academic report series (VI) - autonomous driving on the journey to full autonomy
- ViewModelProvider. Of obsolete solution
- 子组件传递给父组件
- $refs:组件中获取元素对象或者子组件实例:
- .net core 访问不常见的静态文件类型(MIME 类型)
- Algorithm --- bit count (kotlin)
- 关于数据库数据转移的问题,求各位解答下
- How Oracle backs up indexes
猜你喜欢
随机推荐
Paranoid unqualified company
sql中对集合进行非空校验
数据资产管理与数据安全国内外最新趋势
Basic process of network transmission using tcp/ip four layer model
How can flinksql calculate the difference between a field before and after update when docking with CDC?
FPGA course: application scenario of jesd204b (dry goods sharing)
Advantages of using net core / why
Jetpack compose is much more than a UI framework~
Academic report series (VI) - autonomous driving on the journey to full autonomy
The startup of MySQL installed in RPM mode of Linux system failed
$refs:组件中获取元素对象或者子组件实例:
Get the city according to IP
MySQL view bin log and recover data
请问 flinksql对接cdc时 如何实现计算某个字段update前后的差异 ?
Big coffee gathering | nextarch foundation cloud development meetup is coming
IP address
Communication between non parent and child components
Tool class: object to map hump to underline underline hump
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
Config分布式配置中心