ES6 (7):Reflect

Reflect proxy operation target with more of the functions.

Most of the global Reflect the method or static method consistent with the proxy.

Object return value compared to more reasonable: to modify some Objectmethod returns the result, let it become more reasonable. For example, Object.defineProperty(obj, name, desc)when the property can not be defined, it will throw an error, and Reflect.defineProperty(obj, name, desc)will returnfalse .

 

If the  Proxyobject and the  Reflectobject is used in conjunction with the former intercept the assignment, which is the default behavior of the completion of the assignment, and was introduced to receiverthen Reflect.settrigger Proxy.definePropertyinterception.

let p = {
  a: 'a'
};

let handler = {
  set(target, key, value, receiver) {
    console.log('set');
    Reflect.set(target, key, value, receiver)
  },
  defineProperty(target, key, attribute) {
    console.log('defineProperty');
    Reflect.defineProperty(target, key, attribute);
  }
};

let obj = new Proxy(p, handler);
obj.a = 'A';
// set
// defineProperty

 Because Proxy.setthe receiverarguments are always pointing to the current  Proxyinstance (ie example obj), and Reflect.setonce passed receiver, it will be assigned to the property receiverabove (ie obj), leading to trigger definePropertyinterception. If Reflect.setnot passed receiver, then it will not trigger definePropertyinterception.

 

 

Reflect.construct: equivalent to the new target (args) creates an instance of an object.

the Greeting function (name) { 
  this.name = name; 
} 

// new new wording 
const instance = new Greeting ( 'John Doe'); 

// Reflect.construct wording 
const instance = Reflect.construct (Greeting, [ ' Joe Smith ']);

 If the Reflect.construct()first method of argument is not a function , it will error .

 

Reflect.getPrototypeOf:For reading an object __proto__attribute corresponds Object.getPrototypeOf(obj).

new new FancyThing myObj = const (); 

// old wording 
Object.getPrototypeOf (myObj) === FancyThing.prototype; 

// new wording 
Reflect.getPrototypeOf (myObj) === FancyThing.prototype;

 Reflect.getPrototypeOfAnd Object.getPrototypeOfa difference is that if the parameter is not an object, Object.getPrototypeOfwill this parameter into an object, and then run, and Reflect.getPrototypeOfwill complain .

 

 

Reflect.apply (): If you want to bind a function of thisthe object can be written fn.apply(obj, args), but if the function defines its ownapply method, it can only be writtenFunction.prototype.apply.call(fn, obj, args) using Reflectobjects can simplify this operation.

 

Reflect.defineProperty: to define the properties of the object .

const p = new Proxy({}, {
  defineProperty(target, prop, descriptor) {
    console.log(descriptor);
    return Reflect.defineProperty(target, prop, descriptor);
  }
});

p.foo = 'bar';
// {value: "bar", writable: true, enumerable: true, configurable: true}

p.foo // "bar"

 Proxy.definePropertyAssignment of property set to intercept, then Reflect.definePropertycompleted the assignment .

 

 

Reflect.preventExtensions: to make an object becomes unavailable extension. It returns a Boolean value that indicates whether the success of the operation.

 

Observer Pattern: Use a Proxy write observer mode easiest to achieve, namely to achieve observableand observethese two functions. Thinking is observablethe function returns the original object of a Proxy proxy, intercepting assignment, triggering various functions act as an observer .

const queuedObservers = new Set();

const observe = fn => queuedObservers.add(fn);
const observable = obj => new Proxy(obj, {set});

function set(target, key, value, receiver) {
  const result = Reflect.set(target, key, value, receiver);
  queuedObservers.forEach(observer => observer());
  return result;
}

 First: need to define Observable, define the proxy (need to add the proxy object), the execution set (the Add example to set objects to add) operation, the proxy object is first set properties and values ​​in the target object (the original object), and then recycled observer function execution (Print method example).

Observable Person = const ({ 
  name: 'John Doe', 
  Age: 20 is 
}); 

function Print () { 
  the console.log (PERSON.NAME} {$ `,` $ {person.age}) 
} 

the observe (Print); 
person.name = 'John Doe'; 
// output 
// John Doe, 20

 

Guess you like

Origin www.cnblogs.com/jony-it/p/10967729.html