This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

The concept of the proxy

Proxy can be understood as a layer of “interception” before the target object. All external access to the object must pass this layer of interception. Therefore, Proxy provides a mechanism for filtering and rewriting external access. The word Proxy is used to mean that it acts as a Proxy for certain operations.

var proxy = new Proxy(target, handler); 
Copy the code

The new Proxy() parameter generates a Proxy instance, the target parameter represents the target object to intercept, and the handler parameter is also an object used to customize the interception behavior

Proxy instances can also serve as prototype objects for other objects.

var proxy = new Proxy({}, {
  get: function(target, propKey) {
    return 35; }});let obj = Object.create(proxy);
obj.time / / 35
Copy the code

In the above code, the proxy object is the prototype of the OBJ object. The OBJ object itself does not have the time attribute, so according to the prototype chain, the proxy object will read this attribute, resulting in interception.

The same interceptor function can be set to intercept more than one operation.


13 interception operations supported by Proxy:

  • get(target, propKey, receiver): intercepts the reading of object properties, such asproxy.fooandproxy['foo'].
  • set(target, propKey, value, receiver): Intercepts the setting of object properties, such asproxy.foo = vorproxy['foo'] = v, returns a Boolean value.
  • has(target, propKey)Intercept:propKey in proxyReturns a Boolean value.
  • deleteProperty(target, propKey)Intercept:delete proxy[propKey]Returns a Boolean value.
  • ownKeys(target)Intercept:Object.getOwnPropertyNames(proxy),Object.getOwnPropertySymbols(proxy),Object.keys(proxy),for... inLoop to return an array. This method returns the property names of all of the target object’s own properties, andObject.keys()The return result of the object 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), returns 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, there are two additional operations that can be intercepted.
  • apply(target, object, args): Intercepts operations called by Proxy instances as functions, such asproxy(... args),proxy.call(object, ... args),proxy.apply(...).
  • construct(target, args): intercepts operations called by Proxy instances as constructors, such asnew proxy(... args).

Proxy instance method

get()

The get method intercepts a read of a property and can take three parameters, the target object, the property name, and the proxy instance itself (strictly speaking, the object against which the action is directed), the last of which is optional.

set()

The set method is used to intercept the assignment of an attribute. It can take four parameters: the target object, the attribute name, the attribute value, and the Proxy instance itself. The last parameter is optional.

Given that the Person object has an age attribute, which should be an integer not greater than 200, you can use Proxy to ensure that the value of the age attribute meets the requirements.

apply()

The Apply method intercepts function calls, calls, and apply operations.

The Apply method takes three parameters: the target object, the target object’s context object (this), and the target object’s parameter array.

has()

The has() method is used to intercept the HasProperty operation, which takes effect when determining whether an object has a property. A typical operation is the IN operator.

The has() method can take two parameters, the target object and the name of the property to be queried.

construct()

The construct() method is used to intercept the new command

deleteProperty()

The deleteProperty method intercepts the DELETE operation. If the method throws an error or returns false, the current property cannot be deleted by the delete command.

defineProperty()

The defineProperty() method intercepts the Object.defineProperty() operation

getOwnPropertyDescriptor()

GetOwnPropertyDescriptor intercept () method of the Object. GetOwnPropertyDescriptor (), returns a property description Object, or undefined.

getPrototypeOf()

The getPrototypeOf() method is used primarily to intercept and retrieve object prototypes. Specifically, intercept the following operations.

  • Object.prototype.__proto__
  • Object.prototype.isPrototypeOf()
  • Object.getPrototypeOf()
  • Reflect.getPrototypeOf()
  • instanceof

Proxy.revocable()

The proxy.revocable () method returns a cancelable Proxy instance


The problem of this

Although Proxy can Proxy the access of the target object, it is not a transparent Proxy of the target object, that is, without any interception, it cannot guarantee the consistency with the behavior of the target object. The main reason is that in the case of Proxy, the this keyword inside the target object points to Proxy.


Proxies can also be used to implement the ORM layer of a database