1, Reflect
objects and Proxy
objects, but also objects in order to operate the new API ES6 provided. Reflect
Object designed to have several.
- The
Object
methods belonging to significantly internal language object (for exampleObject.defineProperty
), placedReflect
on the object. - Modify certain
Object
method 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, andReflect.defineProperty(obj, name, desc)
will returnfalse
. - Let
Object
operations becomes a function of behavior. SomeObject
operations are imperative, such asname in obj
anddelete obj[name]
, andReflect.has(obj, name)
, andReflect.deleteProperty(obj, name)
let them into the behavior of functions. Reflect
The method of the object andProxy
object methods to-one correspondence, as long asProxy
the object of the method can inReflect
a corresponding way to find the object. This allowsProxy
objects can easily call the correspondingReflect
method, complete the default behavior, as a basis for modifying behavior. In other words, no matterProxy
how to modify the default behavior, you can alwaysReflect
get the default behavior.
2, Reflect.get
the method finds and returns the target
object's name
attributes, if not the property is returned undefined
.
3, Reflect.set
a method is provided target
an object name
property equal value
. If the name
property is set to the assignment function, the function is assigned this
binding receiver
.
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
4, Reflect.has
a method corresponding to name in obj
the inside of the in
operator.
5, Reflect.deleteProperty
a 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.construct
a method is equivalent to new target(...args)
, the use of which is provided a not new
to call a constructor method.
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 ']);
7, Reflect.defineProperty
a 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.defineProperty
in 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 observable
and observe
these two functions. The idea is observable
the function returns the original object of a Proxy proxy, intercepting assignment, triggering various functions act as 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; }
In the above code, we define a first Set
set, all observers functions into this set. Then, observable
the function returns the original object proxy, intercepting assignment. Intercept function set
in, automatically performs all observers.