当前位置:网站首页>Metaprogramming, proxy and reflection
Metaprogramming, proxy and reflection
2022-04-23 17:15:00 【MiMenge】
Metaprogramming
A programming language can design some internal processes through its own language , This can be called Metaprogramming , Such a language can be called
Metalanguage .
The operation object of general code is data
The object of metaprogramming operation is other code
Proxy
Can be used to add proxies to objects , Custom logic in the process of performing an operation on object data , It can intercept changes in data
Yes proxy Operation can have interception effect , Not the target .
Use
adopt new Key word call
Such as :
let obj = {
prop1: 'p1',
prop2: 'p2'
}
// agent obj Object's prop1 attribute
let p = new Proxy(obj, 'prop1', {
set(target, prop, value){
// operation
},
get(target, prop){
return 12222
}
});
// obtain p.prop1 Will trigger get, Set up p.prop1 Will trigger set, Setting or getting other properties does not trigger the agent
// agent obj All attributes
let p = new Proxy(obj, {
set(target, prop, value){
// target --- Target audience
// prop --- Properties of the agent
// value --- Set the value of the
// operation
},
get(target, prop){
return 12222
}
});
Example method
get(target, propKey, receiver)
Block the reading of object properties , such as proxy.foo and proxy[‘foo’].
set(target, propKey, value, receiver)
Setting of intercepting object properties , such as proxy.foo = v or proxy[‘foo’] = v, Returns a Boolean value .
has(target, propKey)
Intercept propKey in proxy The operation of , Returns a Boolean value .
deleteProperty(target, propKey)
Intercept delete proxy[propKey] The operation of , Returns a Boolean value .
ownKeys(target)
Intercept Object.getOwnPropertyNames(proxy)、Object.getOwnPropertySymbols(proxy)、Object.keys(proxy)、for…in loop , Returns an array . This method returns the property names of all the properties of the target object , and Object.keys() The return result of contains only the traversable properties of the target object itself .
getOwnPropertyDescriptor(target, propKey)
Intercept Object.getOwnPropertyDescriptor(proxy, propKey), Returns the description object of the property .
defineProperty(target, propKey, propDesc)
Intercept Object.defineProperty(proxy, propKey, propDesc)、Object.defineProperties(proxy, propDescs), Returns a Boolean value .
preventExtensions(target)
Intercept Object.preventExtensions(proxy), Returns a Boolean value .
getPrototypeOf(target)
Intercept Object.getPrototypeOf(proxy), Return an object .
isExtensible(target)
Intercept Object.isExtensible(proxy), Returns a Boolean value .
setPrototypeOf(target, proto)
Intercept Object.setPrototypeOf(proxy, proto), Returns a Boolean value . If the target object is a function , Then there are two additional operations to intercept .
apply(target, object, args)
Intercept Proxy Instance as an operation of a function call , such as proxy(…args)、proxy.call(object, …args)、proxy.apply(…).
construct(target, args)
Intercept Proxy Operation called by instance as constructor , such as new proxy(…args).
Reflect
mdn describe : Reflect Is a built-in object , It provides interception JavaScript How to operate
effect
In order to better unify the methods of directly operating objects , Es6 A built-in object is introduced in Reflect( A more unified concept of reflection , Before js It has the ability of reflection, but there is no unified concept of reflection object ), This reflect The attribute in the object is the corresponding reflection Api.
And to modify some Object Return value of method , Make it more reasonable
Why use
At present, some methods for internal operation of objects Object and Reflect both , But in the future, such properties will be mounted in Reflect On the object
in other words , All methods within the language will pass Reflect obtain .
Such as :
// I used to write
Object.defineProperty();
// Now write
Reflect.defineProperty();
What is reflection ?
It means that the program can access 、 The ability to detect and modify its own state or behavior
Use
Out of commission new Method call , Cannot call... By function call , Because it's not a function , It's a built-in object
// Use
Reflect.set();
It can be divided into parts that are original Object The method on the , It was transferred to Reflect On , And made a small change , Make the method more reasonable .
Reflect.defineProperty
And Object.defineProperty similar , But when an object cannot be defined Object.defineProperty Will report a mistake and Reflect.defineProperty Can't , It will be returned false, Return on success true, If it's not the object, it will still report an error .Reflect.getPrototypeOf(target)
And Object.getPrototypeOf equally , Returns the prototype of the specified object .Reflect.setPrototypeOf(target, prototype)
And Object.setPrototypeOf equally , It sets the prototype of the specified object to another object .Reflect.getOwnPropertyDescriptor()
And Object.getOwnPropertyDescriptor equally , If there is... In the object , The property descriptor of the given property is returned .Reflect.isExtensible(target)
And Object.isExtensible similar , Determine whether an object is extensible ( Can I add new properties to it ), The difference is , When the parameter is not an object ( Original value ),Object Force it into an object ,Reflect It's a direct error report .Reflect.preventExtensions(target)
And Object.preventExtensions similar , Prevent new properties from being added to objects , The difference is the same as the previous one .apply(func, thisArg, args)
And Function.prototype.apply.call(fn, obj, args) equally .Reflect.ownKeys(target)
And Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target)) equally , Return a containing all its own attributes ( Does not contain inherited properties ) Array of
The other part is the function of the original operator , Become a functional behavior .
Reflect.ihas(target, key)
And in The operators are the same , Let the judgment operation become the function behavior .Reflect.ideleteProperty(target, key)
And delete The operators are the same , Make deletion a functional behavior , Returns a Boolean value for success or failure .Reflect.iconstruct(target, argumentsList[, newTarget])
And new The operators are the same ,target Constructors , The second parameter is an array of constructor parameter classes , The third is new.target Value .Reflect.iget(target, key[, receiver])
And obj[key] equally , The third parameter is to be taken key Deployed getter when , Access to its function this Bind as receiver object .Reflect.iset(target, key, value[, receiver])
Set up target Object's key Attribute is equal to the value, The third parameter is sum set equally . Returns a Boolean value .
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553027238.html
边栏推荐
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- About stream flow, write it down briefly------
- Redis docker installation
- Preliminary understanding of promse
- PostgreSQL column storage and row storage
- Aiot industrial technology panoramic structure - Digital Architecture Design (8)
- 1-3 components and modules
- 自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
- VsCode-Go
- XTask与Kotlin Coroutine的使用对比
猜你喜欢

Get the column name list of the table quickly in Oracle

Detailed explanation of C webpai route

ASP. Net core dependency injection service life cycle

1-4 configuration executable script of nodejs installation

VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
![Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers](/img/65/89473397da4217201eeee85aef3c10.png)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers

基于51单片机红外无线通讯仿真

Scope and scope chain in JS

Bottom processing of stack memory in browser

Lock锁
随机推荐
线性代数感悟之1
Further study of data visualization
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Linux MySQL data timing dump
[WPF binding 3] listview basic binding and data template binding
Promise (I)
[PROJECT] small hat takeout (8)
AIOT产业技术全景结构-数字化架构设计(8)
Use of shell sed command
MySQL restores data through binlog file
[simple understanding of database]
[markdown notes]
1-1 NodeJS
PostgreSQL column storage and row storage
Baidu Map Case - Zoom component, map scale component
手写事件发布订阅框架
Input file upload
JS to find the character that appears three times in the string
Some problems encountered in recent programming 2021 / 9 / 8
Clickhouse SQL operation