Outline
Reflect API ES6 is introduced to the operation target.
Reflect can be used to obtain the behavior of a target object, which is similar to the Object, it is the corresponding Proxy method.
Basic usage:
1, Reflect . GET ( target , name , Receiver ) finds and returns the target object's name attribute.
Exam = the let {
name: "Tom" ,
Age: 24 ,
GET info () {
return the this this.name + .age; } } Reflect.get (Exam, 'name'); // "Tom"
// when target the method of getter presence attributes in a subject, the method of this getter binds Receiver the let Receiver = { name: "Jerry" , Age: 20 is } Reflect.get (Exam, 'info', Receiver); // Jerry20
2, Reflect.set (target, name, value, receiver) returns a boolean type
let exam = {
name: "Tom",
age: 24,
set info(value){
return this.age = value;
}
}
exam.age; // 24
Reflect.set(exam, 'age', 25); // true
console.log(exam.age); // 25
Receiver = the let { // When a property setter methods exist in the target object, this method setter binds Receiver
Age: 18 is
}
Reflect.set (Exam, 'info',. 1, Receiver); // to true
receiver.age ; // 1
. 3, the Reflect.Has(obj,name) is the name of a function in obj instruction, the name attribute is used to find whether there is the object obj.
……
Combination (with the Reflect Proxy)
Methods Reflect object and methods of the Proxy objects is one to one. Therefore, methods of the Proxy objects can get the default behavior by calling a method Reflect object and then perform additional operations.
let exam = { name: "Tom", age: 24 } let handler = { get: function(target, key){ return Reflect.get(target,key); }, set: function(target, key, value){ Reflect.set(target, key, value); } } let proxy = new Proxy(exam, handler) proxy.name = "Jerry" proxy.name // "Jerry"
Ratio of: get the above method is equivalent to the following method.
GET (target, propKey, Receiver) { return target [propKey]; // here by replacing Reflect.get (target, key), as a result.
}
Observer Pattern Observer
When an object is modified, it is automatically notified dependent objects.
// definition of a set of Set const = queuedObservers new new Set (); // put into the observer functions set Set const the observe Fn = => queuedObservers.add (Fn); // Observable proxy returns the original object, interception assignment Observable obj = = const> new new the Proxy (obj, SET {}); function SET (target, Key, value, Receiver) { // get the object of assignment const = Result Reflect.set (target, Key, value, Receiver) ; // perform all observers queuedObservers.forEach (observer => observer ()); // performs assignment operations return Result; }
Person = const Observable ({ name: 'John Doe' , Age: 20 is }); function Print () { the console.log (Person name `change PERSON.NAME} {` $); // `` $ {} es6 string is new in the method, to replace the string, the string is introduced in the variable. } The observe (print); // when the person's attribute changes, automatic notification to print the print information. (The Observer pattern) PERSON.NAME = 'John Doe'; // Person name changed to John Doe