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
Reflect
Objects Proxy
, like objects, are a new API provided by ES6 for manipulating objects. Reflect
Objects are designed for several purposes.
(1) Put Object
some methods of the object that are obviously internal to the language (for example Object.defineProperty
), onto Reflect
the object. At this stage, some methods are deployed on both Object
and Reflect
objects, and new methods in the future will only be deployed on Reflect
objects. That is, Reflect
the methods inside the language can be obtained from the object.
(2) Modify Object
the 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 Object
operations become functional behaviors. Some Object
operations are imperative, like name in obj
sum 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) Reflect
The method of the Proxy
object corresponds to the method of the object one by one. As long as it is the method of the object, the corresponding Proxy
method can Reflect
be found on the object. This allows the Proxy
object to easily call the corresponding Reflect
method to complete the default behavior as the basis for modifying the behavior. That is, no matter Proxy
how you modify the default behavior, you can always Reflect
get 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 Proxy
method intercepts target
the property assignment behavior of the object. It uses Reflect.set
methods to assign values to object properties, ensuring that the original behavior is done, and then deploying additional functionality.