Application Method and Application Example Analysis of ES6 Proxy and REFLECT

The examples of this document describe the use of ES6 Proxy and Reflect. Share to everyone for your reference, as follows:

PROXY and REFLECT are the new API provided by ES6 for better operation objects, and then explore the role of both.

The role of Proxy, Reflect:

Proxy’s design purpose is (modifying the programming language), modify the default method of operation Behavior,

is equivalent to making a modification in the language level, is meta programming

, for example, modify set, GET method REFLECT:

1, mapping some methods that are obviously within the object language, currently coexisting with

Object and Reflect, the future is only on Reflectles

2, modify the results returned by

Object , reduce abnormal throw, make it more reasonable

3, let

Object Operation varies into function behavior (main role) method of reflect object with

Proxy

object Corresponding (main) Proxy and Reflect method: Example method for proxy:

gET ()

set ()

Apply () preventextensions () Keywords point to Proxy Agent method returns a canceled Proxy instance
DELETEPROPERTY () DefineProperty () GetownPropertyDescriptor () getPrototypeOf () ISEXTENSIBLE ( oKEYS (),
setPrototypeOf () VAR PERSON = {Name: “Zhang 3”}; var proxy = new proxy (Person, {GET: FUNCTION (TARGET, Property) {if (Property In Target) {Return Target [Property];} else {throw new reasonerror (“Property \” + Property + “\” DOES NOT EXIST. “);}} }); Note: Interior of the target object proxy.revocable
Reflectic method:
  and proxy corresponds, reflecting has 13 static methods, each corresponds to Proxy Example method  

Reflect.Apply (THISARG, ARGS) Reflect.construct (Target , args)

Reflect.Get (target, name, receiver) Reflect (Target, Name , Value, Receiver

Reflect.defineProperty (Target, Name, DESC)

Reflectt.deleteProperty (Target , Name)

Reflect.has (target, name) More about JavaScript Related Contents View this Topic: “JavaScript Operation DOM Skation Summary”, “JavaScript Page Element Operation Skills Summary”, “JavaScript Event Information and Skills”, “JavaScript Find Algorithm Tips Summary “,” JavaScript Data Structure and Algorithm Skill Summary “,” JavaScript Overclic Algorithm and Skills Summary “and” JavaScript Error and Debug Skill Summary “
Reflect.OWNKEYS (Target)
[Target) Reflect.getownpropertyDescriptor (target, name)
Reflect.SetProtoTypeof , Prototype
PROXY and REFLECT contact: Example method of proxy and the static method of REFLECT correspond to
Const QueuedObservers = New set (); const observe = fn => queuedobservers.add (fn); / / Receive a function fnConst Observable = OBJ => New Proxy (Obj, {set}); // Receive an object OBJ, use the set function to block the OBJ Function Set (Target, Key, Value, Receiver) {const result = reflect. Set (Target, Key, Value, Receiver; QueuedObservers.Foreach (Observer => Observer ()); returnrate;} Interested friends can use online HTML / CSS / JavaScript code run tool : http: //tools.jb51.net/code/htmljsrun Tested the code running effect.
I hope this article will help you JavaScript programming.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment