当前位置:网站首页>JS proxy
JS proxy
2022-06-30 04:09:00 【Runqing】
Proxy Object is used to create a proxy for an object , So as to achieve the interception and customization of basic operations ( Such as attribute search 、 assignment 、 enumeration 、 Function calls, etc ).
grammar
// target: To use Proxy The target object of the wrapper ( It can be any kind of object , Including native arrays , function , Even another agent ).
// handler: An object , The property value of this object is usually a function , When various operations of the proxy are defined p act . The attribute names on the object are specific and optional , If there is no attribute , The default behavior of the source object is preserved .
const p = new Proxy(target, handler)
handler There are ways to object :
These methods and Reflect There is an obvious correspondence between the methods of :
// Intercept Proxy Instance as an operation of a function call , such as proxy(...args)、proxy.call(object, ...args)、proxy.apply(...).
handler.apply(target, thisArg, args)
// Intercept Proxy Operation called by instance as constructor , such as new proxy(...args).
handler.construct(target, args)
// Block the reading of object properties , such as proxy.foo and proxy['foo'].
handler.get(target, name, receiver)
// Setting of intercepting object properties , such as proxy.foo = v or proxy['foo'] = v, Returns a Boolean value .
handler.set(target, name, value, receiver)
// Intercept Object.defineProperty(proxy, propKey, propDesc)、Object.defineProperties(proxy, propDescs), Returns a Boolean value .
handler.defineProperty(target, name, desc)
// Intercept delete proxy[propKey] The operation of , Returns a Boolean value .
handler.deleteProperty(target, name)
// Intercept propKey in proxy The operation of , Returns a Boolean value .
handler.has(target, name)
// 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 .
handler.ownKeys(target)
// Intercept Object.isExtensible(proxy), Returns a Boolean value .
handler.isExtensible(target)
// Intercept Object.preventExtensions(proxy), Returns a Boolean value .
handler.preventExtensions(target)
// Intercept Object.getOwnPropertyDescriptor(proxy, propKey), Returns the description object of the property .
handler.getOwnPropertyDescriptor(target, name)
// Intercept Object.getPrototypeOf(proxy), Return an object .
handler.getPrototypeOf(target)
// Intercept Object.setPrototypeOf(proxy, proto), Returns a Boolean value . If the target object is a function , Then there are two additional operations to intercept .
Reflect.setPrototypeOf(target, prototype)
Use scenario example
1. Data is read-only
let data = new Proxy(response.data, {
set(obj, key, value) {
return false
}
})
class Component {
constructor () {
this.proxy = new Proxy({
id: Math.random().toString(36).slice(-8)
})
}
get id () {
return this.proxy.id
}
}
2. data verification
// Validator.js
export default (obj, key, value) => {
if (Reflect.has(key) && value > 20) {
obj[key] = value
}
}
import Validator from './Validator'
let data = new Proxy(response.data, {
set: Validator
})
Create cancelable proxy
Proxy.revocable() Method returns a cancelable Proxy example .
Proxy.revocable() Method returns an object , The object proxy The attribute is Proxy example ,revoke Property is a function , May cancel Proxy example .
When executed revoke After the function , Revisit Proxy example , Will throw a mistake .
Proxy.revocable() A usage scenario for is , The target object does not allow direct access to , Must be accessed through a proxy , Once the visit is over , Take back the agency , Not allowed to visit again .
let target = {
};
let handler = {
};
let {
proxy, revoke} = Proxy.revocable(target, handler);
proxy.foo = 123;
proxy.foo // 123
revoke();
proxy.foo // TypeError: Revoked
边栏推荐
- Day 10 data saving and loading
- [note] on May 27, 2022, MySQL is operated through pychart
- DBT product initial experience
- Smart use of bitmap to achieve 100 million level massive data statistics
- I spent three years in a big factory outsourcing, which subverted my understanding!
- Concatenation of Languages(UVA10887)
- 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
- [note] May 23, 2022 MySQL
- Linear interpolation of spectral response function
- Analysis of similarities and differences of various merged features (Union, merge, append, resolve) in ArcGIS
猜你喜欢

Educoder group purchase suspension box page production

在大厂外包呆了三年,颠覆了我的认知!

ThingsBoard教程(二三):在规则链中计算二个设备的温度差

EasyCVR部署服务器集群时,出现一台在线一台不在线是什么原因?

【论文阅读|深读】Role2Vec:Role-Based Graph Embeddings

How to analyze and solve the problem of easycvr kernel port error through process startup?

巧用 Bitmap 实现亿级海量数据统计

How to use FME to create your own functional software

Day 11 script and game AI

【论文阅读|深读】DANE:Deep Attributed Network Embedding
随机推荐
Error encountered in SQL statement, solve
When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?
How to use FME to create your own functional software
Cloud native -- websocket of Web real-time communication technology
Collinearity problem
SQL追加字段
Node-RED系列(二八):基于OPC UA节点与西门子PLC进行通讯
[operation] MySQL query operation 2 on May 25, 2022
2021-07-14
你清楚AI、数据库与计算机体系
Solutions for project paths
[note] on May 27, 2022, MySQL is operated through pychart
Day 10 data saving and loading
学校实训要做一个注册页面,要打开数据库把注册页面输入的内容存进数据库但是
深入浅出掌握grpc通信框架
华为云原生——数据开发与DataFactory
Node red series (28): communication with Siemens PLC based on OPC UA node
Day 9 script and resource management
Knowledge - how to build rapport in sales with 3 simple skills
Radiant energy, irradiance and radiance