es6+ Proxy and reflect use __proxy

Source: Internet
Author: User
es6+ Proxy and reflect use Description

Proxy translated as "agent", used to modify the default behavior of some operations, you can think that the target object | Function before the set up a layer of "interception", external access, must be intercepted through this layer, the interception can be external access to filter and rewrite 1. Basic Syntax target The target object or function of an agent handler a custom object to intercept an outside operation traps the interception method of the corresponding outside Operation new Proxy () is used to create a proxy instance

Const Handler = {
    [traps] (... args) {
        ...
    }}
;
Const OBJ = new Proxy (target, handler)
2. Supported interception operations Get (target, key, receiver) intercepts the read operation set (target, key, value, receiver) to intercept assignment operations has (target, key) intercept in operator operation (for In loop not counted) Dele Teproperty (target, key) intercept delete operator operation Ownkeys (target) intercept object.getownpropertynames (proxy) | Object.getownpropertysymbols (proxy) | Object.keys (proxy) These three operations Getownpropertydescriptor (target, key) to obtain the key name intercept the action of obtaining the property descriptor Object.getownpropertydescriptor ( Proxy, key) DefineProperty (target, key, description) intercepts the operation that defines the property operator Object.defineproperty (proxy, key, description) | Object.defineproperties (proxy, descriptions) preventextensions (target) blocks an operation that changes an object to an extensible object.preventextensions ( Proxy) isextensible (target) intercepts whether the object is an extensible Operation Object.isextensible (proxy) getprototypeof (target) intercepts operations that return a prototype object. When object.getprototypeof (proxy) apply (target, context, args) intercepts target as a function, the action to invoke the function includes: FN () | Fn.call () | Fn.apply () construct (target, args) intercepts the operation called by the Proxy instance New Fn ()
Const DEF = {_def: ' Def obj '}; Const PROXY = new Proxy (Def, {//Intercept value operation got (target, key) {//Get interception requires return value returned key in target target
   [key]: ' Target: ${json.stringify (target)} not found key: ${key} ';
      },//block assignment operation set (target, key, value) {if (key = = ' age ') {return Console.warn (' secret ');
   } Target[key] = value;
      },//Intercept in operator has (target, key) {if (key[0] = = ' _ ') {return false;
   Return key in Target; },//intercept delete operation, throw error or return false cannot delete DeleteProperty (target, key) {if (key[0] = = ' _ ') {CONSOLE.W
         ARN (' cant delete prop: ${key} ');
      return false;
      } console.log (' delete ', key);
   return true; (), and/or intercept Object.keys (), and so on gets the property name operation, returns each item of the array if this property is not present in the proxy, it will be filtered out Ownkeys (target) {//proxy does not have a attribute so even if written in the return value
   Will not be returned to return [' a ', ' _def '];

}
}); Value Test Console.log (Proxy.color); Target: {"_def": "Def obj"} not FOund Key:color//Assignment Test proxy.age = 90;
Age is a secret Proxy.name = ' small white '; Console.log (json.stringify (proxy)); {"_def": "Def obj", "name": "Small White"}//In Test Console.log (' _def ' in proxy); False for (key in proxy) {Console.log (key);///_def name has block for for-in does not take effect}//delete test delete proxy._def; Can ' t delete prop: _def//Object.keys () test Console.log (Object.keys (proxy));  ["_def"]/* =========== ============ ============ ============ ===========/Const DEF = function (A, b) {return
A * b;

}; Const DEF = new Proxy (Def, {//Intercept function call apply (target, context, args) {Console.log (context, args, target (... arg)
      s));
   Return target (... args);  Construct (target, args) {if (Args.length < 2) {Console.warn (' The arguments is not
      Enough ') return {value:null};
   return {value:target (... args)};

}
}); The test function calls Def (90, 20);

undefined [m] 1800 Const CTX = {name: ' CTX '}; Def.call (CTX, 90, 20, 30); {Name: "CTX"} [90, 20, 30] 1800//test constructor calls new Def (79); The arguments is not enough console.log (new Def (89, 27));

 {value:2403}
3. Reflect Reflex

The reflect object's method corresponds to the proxy object's method one by one, so that regardless of how the default behavior is modified on proxy, the default behavior can be accomplished by calling the reflect method

Const DEF = {
   _def: ' Def '
};

Const PROXY = new Proxy (Def, {get
   (target, key) {
      Console.log (target, Target[key]);
      Return Reflect.get (target, key); Triggers the default action
   }
});

Console.log (PROXY._DEF); Def

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.