Meet ES6 of Reflect and Proxy

Reflect

Reflect Object method to replace many of the methods clearly belongs to the object Object made inside the object placed on Reflect, Method 13

Reflect.apply(target, thisArg, args)
Reflect.construct(target, args)
Reflect.get(target, name, receiver)
Reflect.set(target, name, value, receiver)
Reflect.defineProperty(target, name, desc)
Reflect.deleteProperty(target, name)
Reflect.has(target, name)
Reflect.ownKeys(target)
Reflect.isExtensible(target)
Reflect.preventExtensions(target)
Reflect.getOwnPropertyDescriptor(target, name)
Reflect.getPrototypeOf(target)
Reflect.setPrototypeOf(target, prototype)

 Reflect the first parameter in all methods have the object must not be a simple data type, such as 1, true; 

Reflect.apply方法等同于Function.prototype.apply.call(func, thisArg, args),用于绑定this对象后执行给定函数.
Function.prototype.apply.call <===> Reflect.apply

Reflect.getPrototypeOfObject.getPrototypeOf的一个区别是,如果参数不是对象,Object.getPrototypeOf会将这个参数转为对象,然后再运行,而Reflect.getPrototypeOf会报错。

Proxy

Proxy agent may object (not a simple data types such as 1, true, 'string' Proxy settings.) The behavior of some of the cross-sectional access using this filter and can be rewritten to access outside.

Proxy constructor, pass two parameters, the first parameter is an object filter to be intercepted, the second parameter is an object that specifies the behavior of which contact the cross-sectional

const obj ={
  name:'',age:'18',
  get showName() {return `${this.name}年龄${this.age}`}  
}

const proxyOpr = {
 get: function(target, key, receiver){
     console.log(`getting ${key}`);
      return Reflect.get(target, key, receiver);
 },
  set: function(target, key, value, receiver){
    console.log(`setting ${key} as '${value}'`);
     return Reflect.set(target, key, value, receiver); 
  } 
}

const proxy = new Proxy(obj, proxyOpr);
proxy.name='test proxy';
console.log(proxy.showName);

// setting name as 'test proxy'

// getting showName
// getting name
// getting age
// test proxy年龄18

The value of obj and assignment operator are intercepted and added a log to print.

important point:

And apply filters when 1 get there, we will go get filtered.

2 generated proxy objects will achieve this.

3 can be used to cancel the proxy Proxy.revocable

let target = {};
let handler = {};

let {proxy, revoke} = Proxy.revocable(target, handler);

proxy.foo = 123;
proxy.foo // 123

revoke(); //取消代理
proxy.foo //TypeError: Revoked

 new Proxy () the data type object is created and a value of the target object, i.e. the target object is a funtion, it is also a proxy function

const proxy1 = new Proxy(function(i, j){return i+j;},
{
  apply: function(target, thisBinding, args){
    console.log(args);
    return Reflect.apply(target, thisBinding, args); //切记写return 
  }
})
const t = proxy1(1, 2);
console.log(t);
typeof proxy1
// [1,2]
// 3
// "function"

 Proxy instance can be used as a prototype to another object

var proxy = new Proxy({}, {
  get: function(target, property) {
    return 35;
  }
});

var obj ={name:'tt'};
Reflect.setPrototypeOf(obj,proxy)

obj.name // tt
obj.age //35

  age itself is not the object obj property, so the chain according to the prototype, reads the property on the proxy objects, resulting in being intercepted. obj itself has the property, can not be intercepted read.

 

Agents can make the target object appears only focus on their own behavior, to communicate with the outside world can be put in proxy to complete. For example, a cat called rat run. In fact, the cat can not know whether there are mice or other animals, only to realize their call on it. In the end who can run to the cat's agent to complete. Acting was intercepted meow, cat food chain can be added to all the animals running behavior.

Reflect and methods in the Proxy can intercept is one to one. As long as the Proxyobject's methods, you can in Reflecta corresponding way to find the object.

Somewhere I think js proxy agent with the spring in somewhat similar, can learn to compare.

 

Guess you like

Origin www.cnblogs.com/Gift/p/10826089.html