ES6-Proxy, Proxy

Es6 enhanced proxy agent objects and functions (methods)
 
Proxy used to modify the default behavior of some operations that modify the programming language level, a "meta-programming"
Proxy meaning "agent", namely the establishment of a "block" before accessing objects, any access to the object of the operation
Through this channel before will "block", i.e., to perform the method defined inside the Proxy.
 
 
Proxy Statement braces put on a target body a second pre-release mechanisms     get set apply
Pro = the let new new the Proxy ({
     // Method body 
    the Add: function (Val) {
         return Val + 100 ; 
    }, 
    name: ' Ananiah ' 
}, { 
    // pretreatment SET mechanism Apply get
     // front get property obtained pretreatment 
    get : function (target, Key, Property) { 
        the console.log (target); // {the Add: ƒ, name: "Ananiah"} 
        the console.log (Key); // name 
        return target [Key] 
    }, 
    SET : function ( target, Key, value, Receiver) { 
        the console.log ( `Key Setting $ {} = {value} $`); //Agent name = Setting 

        // print out will change after the return 
        return target [Key] + = value ' 123 ' ; 
    } 
}); 
the console.log (pro.name); 
pro.name = ' agent ' ; 
the console.log (pro.name);   // name proxy 123 

// Apply 
the let target = function () {
      return   ' Ananiah ' ; 
} 
the let Handler = { 
    Apply (target, CTX, args) { 
        the console.log ( ' Apply Start ' );
         return Reflect.apply(...arguments); //apply start
    }
};
let proxx = new Proxy(target,handler);
//调用
console.log(proxx()) //ananiah

 

 

Guess you like

Origin www.cnblogs.com/Ananiah/p/11073690.html