当前位置:网站首页>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
边栏推荐
- Calculation formula related to tolerance analysis
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- [PROJECT] small hat takeout (8)
- Read a blog, re understand closures and tidy up
- For the space occupation of the software, please refer to the installation directory
- Clickhouse - data type
- 线性代数感悟之2
- Get the column name list of the table quickly in Oracle
- Use of shell awk command
- About stream flow, write it down briefly------
猜你喜欢
Document operation II (5000 word summary)
Scope and scope chain in JS
[registration] tf54: engineer growth map and excellent R & D organization building
Milvus 2.0 質量保障系統詳解
Signalr can actively send data from the server to the client
Sub database and sub table & shardingsphere
For the space occupation of the software, please refer to the installation directory
C语言函数详解
Webapi + form form upload file
groutine
随机推荐
EF core in ASP Generate core priority database based on net entity model
[problem solving] [show2012] random tree
Net standard
Shell-sort命令的使用
MySQL master-slave configuration under CentOS
Generate random numbers with high quality and Gaussian distribution
New keyword learning and summary
About stream flow, write it down briefly------
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Collect blog posts
Paging SQL
How does matlab draw the curve of known formula and how does excel draw the function curve image?
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
Bottom processing of stack memory in browser
AIOT产业技术全景结构-数字化架构设计(8)
Model problems of stock in and stock out and inventory system
Document operation II (5000 word summary)
First knowledge of go language
[registration] tf54: engineer growth map and excellent R & D organization building
Promise (IV)