005-Symbol、Proxy、Reflect

1、Symbol:http://es6.ruanyifeng.com/#docs/symbol

2、Proxy:http://es6.ruanyifeng.com/#docs/proxy

  Proxy is used to modify the default behavior of certain operations, which is equivalent to making changes at the language level, so it belongs to a kind of "meta programming", that is, programming a programming language.

  Proxy can be understood as setting up a layer of "interception" before the target object, and access to the object from the outside world must pass this layer of interception first, so it provides a mechanism to filter and rewrite the access to the outside world. The original meaning of the word Proxy is a proxy, and it is used here to indicate that it is used to "proxy" some operations, which can be translated as "proxy".

3、Reflect:http://es6.ruanyifeng.com/#docs/reflect

  ReflectObjects Proxy, like objects, are a new API provided by ES6 for manipulating objects. ReflectObjects are designed for several purposes.

(1) Put Objectsome methods of the object that are obviously internal to the language (for example Object.defineProperty), onto Reflectthe object. At this stage, some methods are deployed on both Objectand Reflectobjects, and new methods in the future will only be deployed on Reflectobjects. That is, Reflectthe methods inside the language can be obtained from the object.

(2) Modify Objectthe return results of some methods to make them more reasonable. For example, Object.defineProperty(obj, name, desc)when the property cannot be defined, an error will be thrown, and an error Reflect.defineProperty(obj, name, desc)will be returned false.

// 老写法
try {
  Object.defineProperty(target, property, attributes);  // success } catch (e) {  // failure }  // 新写法 if (Reflect.defineProperty(target, property, attributes)) {  // success } else {  // failure } 

(3) Let Objectoperations become functional behaviors. Some Objectoperations are imperative, like name in objsum delete obj[name], and Reflect.has(obj, name)sum Reflect.deleteProperty(obj, name)makes them functional behaviors.

// 老写法
'assign' in Object // true
 // 新写法 Reflect.has(Object, 'assign') // true 

(4) ReflectThe method of the Proxyobject corresponds to the method of the object one by one. As long as it is the method of the object, the corresponding Proxymethod can Reflectbe found on the object. This allows the Proxyobject to easily call the corresponding Reflectmethod to complete the default behavior as the basis for modifying the behavior. That is, no matter Proxyhow you modify the default behavior, you can always Reflectget the default behavior on .

Proxy(target, {
  set: function(target, name, value, receiver) { var success = Reflect.set(target,name, value, receiver); if (success) { log('property ' + name + ' on ' + target + ' set to ' + value); } return success; } }); 

In the above code, the Proxymethod intercepts targetthe property assignment behavior of the object. It uses Reflect.setmethods to assign values ​​to object properties, ensuring that the original behavior is done, and then deploying additional functionality.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209684&siteId=291194637