Novice tutorial: www.runoob.com/w3cnote/es6… Proxy is to set up a layer of “interception” before the target object, and the external access to the object must pass this layer of interception first. Therefore, it provides a mechanism to filter and rewrite the external access.
To create the agent
Using a Proxy in ES6 is done via a new Proxy instance object: new Proxy(target,handler)
Parameter description:
- Target: Indicates the target object to intercept (the proxy)
- Handler: a configuration object that provides a function that intercepts each agent operation.
Interception operation method of Proxy
`
<span style="color: RGB (72, 11, 184);" ><strong>      < span style = "box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; white-space: inherit! Important;" Qiao Cao ', nativeplace: 'Peiguo County ', password:' chicken ribs'}; Let bt = document.querySelector('.st'); let pd = document.querySelector('.gt'); Var setname = function () {let setkey = prompt(' enter key to modify '); if (setkey == 'password') { proxy1.password = setkey; } else { let val = obj[setkey]; If (val == undefined) {alert 'if (val == undefined); setname(); } else {let newvalue = prompt(' enter new key values '); proxy1[setkey] = newvalue; return; }}; }; Var getpd = function () {let getkey = prompt(' enter the key to get '); If (getkey == 'password') {let pd = prompt(' enter password'); if (pd ! = 'alert ') {alert' password error!! `; } } else { let val = obj[getkey]; If (val == undefined) {alert 'if (val == undefined) {alert' return; } else {let hint = '${getkey} ${obj[getkey]}'; let p = document.querySelector('p'); p.innerHTML = `<span style="color:red; font-size:30px;" >${hint}</span>`; return; }}}; let handler = { get: function (traget, key) { getpd(); }, set: function (key, key, value) {if (key == 'password') {alert 'you have no right to change password'; } else {if (value == traget[key]) {alert ' setname(); } else if (value == ") {alert '= null'; setname(); } else { console.log(value); Alert 'Modified successfully'; }}}}; Let proxy1 = new Proxy(obj, handler); bt.onclick = function () { setname(); }; pd.onclick = function () { getpd(); }; </script>Copy the code
Get (target, propKey, Receiver) : intercepts the reading of object properties.
The last of the three parameters, the target object, the property name, and the Proxy instance itself (strictly speaking, the object against which the action is directed, the so-called sink), is optional.
set()
Set (target, Key, value,receiver) : used to intercept the assignment of a property, filter, process the data to be assigned, or set the permission
Four parameters, the target object, the property name, the property value, and the Proxy instance itself. The last parameter is optional.
Note that in strict mode (‘use strict’;) , the set agent will report an error if it does not return true.
apply()
Apply (target, object, args) : Intercepts Proxy instances as function calls
The three arguments are the target object (the target object must be the function) the context object (this) of the target object (which needs to be set manually) and the array of parameters of the target object.
has()
Has (target, propKey) : Intercepts the propKey in proxy operation and returns a Boolean value. The has method takes two parameters, the target object and the name of the property to be queried.
The has method is used to intercept hasProperty operations, that is, to determine whether an object has a property. The typical operation is the in operator (but for… The in operation does not work). The has method intercepts HasProperty operations, not HasOwnProperty operations. That is, the HAS method does not determine whether a property is a property of the object itself or an inherited property.
construct()
Construct (target,args) : used to intercept new commands. The construct method must return an object, otherwise an error will be reported. Two parameters:
Target: Indicates the target object
Args: the argument object to the constructor
deleteProperty()
DeleteProperty (target,key) : intercepts delete operations. If this method throws an error or returns false, the current property cannot be deleted by the delete command.
Note that the properties of the target object that are not configurable and works without additional information cannot be deleted by the deleteProperty method. Otherwise, errors will be reported.
defineProperty()
DefineProperty (target,key, Descriptor) : intercepts Object. DefineProperty operation.
If the target object is non-extensible, then defineProperty cannot add attributes that do not exist on the target object, otherwise an error will be reported. In addition, the defineProperty method cannot change any of the properties of the target object that are not writable or configurable.
getOwnPropertyDescriptor()
GetOwnPropertyDescriptor (target, key) : interception Object. GetOwnPropertyDescriptor (), returns a property description Object, or undefined.
getPrototypeOf()
GetPrototypeOf (target) : Used to intercept and obtain the object prototype. Include:
- Object.prototype.proto
- Object.prototype.isPrototypeOf()
- Object.getPrototypeOf()
- Reflect.getPrototypeOf()
- instanceof
The return value of the getPrototypeOf method must be an object or null, otherwise an error is reported. In addition, if the target object is non-extensible, the getPrototypeOf method must return the prototype object of the target object.
isExtensible()
IsExtensible (target) : intercepts objects. IsExtensible operation
This method can only return a Boolean value, otherwise the return value is automatically converted to a Boolean value. This method has a strong constraint that its return value must be consistent with the isExtensible property of the target object or an error will be thrown.
ownKeys()
OwnKeys (target) : used to intercept the read operation of the object’s own properties. Include:
- Object.getOwnPropertyNames()
- Object.getOwnPropertySymbols()
- Object.keys()
- for… In circulation
When using the object. keys method, there are three types of properties that are automatically filtered by the ownKeys method and do not return. Attribute that does not exist on the target object: Attribute that does not exist on the target object, attribute named Symbol value, and attribute that is not traversable (Enumerable).
The ownKeys method returns array members that can only be strings or Symbol values (although Symbol is ignored). If there are other types of values, or if the return is not an array at all, an error will be reported.
If the target object itself contains a non-configurable property, that property must be returned by the ownKeys method, otherwise an error will be reported.
If the target object is non-extensible, then the array returned by the ownKeys method must contain all the properties of the original object and must not contain redundant properties. Otherwise, an error is reported.
preventExtensions()
PreventExtensions (Target) : Intercept object.preventExtensions (). The method must return a Boolean value, otherwise it will be automatically converted to a Boolean value.
This method has a limitation that proxy.preventExtensions can only return true if the target Object is not extensible (that is, object.isextensible (proxy) is false), otherwise an error will be reported.
setPrototypeOf()
SetPrototypeOf (target,proto) : Used to intercept the object.setPrototypeof method. This method can only return a Boolean value, otherwise it is automatically converted to a Boolean
Proxy.revocable()
Proxy.revocable(traget,handler) : Returns a cancerable Proxy instance.