js-ES6 study notes -Reflect

1, Reflectobjects and Proxyobjects, but also objects in order to operate the new API ES6 provided. ReflectObject designed to have several.

  • The Objectmethods belonging to significantly internal language object (for example Object.defineProperty), placed Reflecton the object.
  • Modify certain 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 return false.
  • Let Objectoperations becomes a function of behavior. Some Objectoperations are imperative, such as name in objand delete obj[name], and Reflect.has(obj, name), and Reflect.deleteProperty(obj, name)let them into the behavior of functions.
  • ReflectThe method of the object and Proxyobject methods to-one correspondence, as long as Proxythe object of the method can in Reflecta corresponding way to find the object. This allows Proxyobjects can easily call the corresponding Reflectmethod, complete the default behavior, as a basis for modifying behavior. In other words, no matter Proxyhow to modify the default behavior, you can always Reflectget the default behavior.

2, Reflect.getthe method finds and returns the targetobject's nameattributes, if not the property is returned undefined.

3, Reflect.seta method is provided targetan object nameproperty equal value. If the nameproperty is set to the assignment function, the function is assigned thisbinding receiver.

Copy the code
var myObject = {
  foo: 4,
  set bar(value) {
    return this.foo = value;
  },
};

var myReceiverObject = {
  foo: 0,
};

Reflect.set(myObject, 'bar', 1, myReceiverObject);
myObject.foo // 4
myReceiverObject.foo // 1
Copy the code

4, Reflect.hasa method corresponding to name in objthe inside of the inoperator.

5, Reflect.deletePropertya method is equivalent to delete obj[name], delete an attribute object.

This method returns a Boolean value. If you delete a success, or deleted property does not exist, return true; deletion fails, the deleted property still exists, return false.

6, Reflect.constructa method is equivalent to new target(...args), the use of which is provided a not newto call a constructor method.

Copy the code
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 ']);
Copy the code

7, Reflect.definePropertya method substantially equivalent to Object.defineProperty, attributes used to define the object. In the future, which will be gradually abolished, please use from now on Reflect.definePropertyin its place.

8, the observer pattern (Observer mode) refers to a function of automatic observation data object, once the object has changed, the function will be executed automatically.

Next, write a Proxy observer mode easiest to achieve, namely to achieve observableand observethese two functions. The idea is observablethe function returns the original object of a Proxy proxy, intercepting assignment, triggering various functions act as observer.

Copy the code
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;
}
Copy the code

In the above code, we define a first Setset, all observers functions into this set. Then, observablethe function returns the original object proxy, intercepting assignment. Intercept function setin, automatically performs all observers.

Guess you like

Origin www.cnblogs.com/sexintercourse/p/12034730.html