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 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, and Reflect.defineProperty(obj, name, desc)
will returnfalse
.
If the Proxy
object and the Reflect
object 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 receiver
then Reflect.set
trigger Proxy.defineProperty
interception.
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.set
the receiver
arguments are always pointing to the current Proxy
instance (ie example obj
), and Reflect.set
once passed receiver
, it will be assigned to the property receiver
above (ie obj
), leading to trigger defineProperty
interception. If Reflect.set
not passed receiver
, then it will not trigger defineProperty
interception.
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.getPrototypeOf
And Object.getPrototypeOf
a difference is that if the parameter is not an object, Object.getPrototypeOf
will this parameter into an object, and then run, and Reflect.getPrototypeOf
will complain .
Reflect.apply (): If you want to bind a function of this
the 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 Reflect
objects 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.defineProperty
Assignment of property set to intercept, then Reflect.defineProperty
completed 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 observable
and observe
these two functions. Thinking is observable
the 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