MoonCheung

MoonCheung

人生路漫漫,何曾有坦途。:)

Understanding how to use a Proxy

  • Proxy is used to modify the default behavior of certain operations. It sets up a layer of "interception" before the target object, so that any access to the object from the outside must first go through this layer of interception. Therefore, it provides a mechanism to filter and rewrite access from the outside world.
  • get property: This method is used to intercept the reading operation of a certain property. It can accept three parameters: the target object, the property name, and the proxy instance itself (optional parameter).
    • If you access a non-existent property of the target object, an error will be thrown. If there is no interception function for this property, accessing a non-existent property will simply return undefined.
  • set property: This method is used to intercept the assignment operation of a certain property. It can accept four parameters: the target object, the property name, the property value, and the proxy instance itself (optional parameter).
  • apply property: This method can accept three parameters: the target object, the context object (this) of the target object, and the array of parameters of the target object.
  • has property: This method is used to intercept the HasProperty operation, which means that when determining whether an object has a certain property, this method will take effect. It can accept two parameters: the target object and the property name to be queried.
  • construct property: This method is used to intercept the new command. It can accept two parameters: the target object and the parameter object of the constructor function.
  • deleteProperty property: This method is used to intercept the delete operation. If this method throws an error or returns false, the current property cannot be deleted by the delete command.
  • defineProperty property: This method intercepts the Object.defineProperty operation.
  • getOwnPropertyDescriptor property: This method intercepts Object.getOwnPropertyDescriptor(), and returns a property descriptor object or undefined.
  • getPrototypeOf property: This method is mainly used to intercept the retrieval of object prototypes. Specifically, it intercepts the following operations:
    • Object.prototype.__proto__
    • Object.prototype.isPrototypeOf()
    • Object.getPrototypeOf()
    • Reflect.getPrototypeOf()
    • instanceof()
  • isExtensible property: This method intercepts the Object.isExtensible operation.
  • ownKeys property: This method is used to intercept the reading operation of object's own properties. Specifically, it intercepts the following operations:
    • Object.getOwnPropertyNames()
    • Object.getOwnPropertySymbols()
    • Object.keys()
    • for...in... loop
  • preventExtensions property: This method intercepts Object.preventExtensions(). It must return a boolean value, otherwise it will be automatically converted to a boolean value.
  • setPrototypeOf property: This method is mainly used to intercept the Object.setPrototypeOf method.
  • Proxy.revocable property: This method returns a revocable Proxy instance.
// get, set methods
let proxy = new Proxy({ }, {
  get: function(target, prop){
    console.log('Setting get operation');
    return target[prop];
  },
  set: function(target, prop, value){
    console.log('Setting set operation');
    target[prop] = value;
  }
})

proxy.time = 35;  // Setting set operation 35
console.log(proxy.time);  // Setting get operation 35

// has method to hide certain properties from being discovered by the in operator
let handler = {
  has: function(target, key){
    if(key[0] === '_'){
      return false;
    }
    return key in target;
  }
}

let target = {
  _prop: 'foo',
  prop: 'foo'
}

let proxy = new Proxy(target, handler);
console.log('__prop' in proxy) // Prints: false;
console.log('prop' in proxy) // Prints: true;
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.