ES15 WeakMap and WeakSet, proxy and reflect

Posted by fansphp on Sat, 15 Jan 2022 19:17:38 +0100

WeakMap/WeakSet

  • The traversal method does not exist on the prototype (the iterator interface is not deployed)
  • Members can only be objects
  • The garbage collection mechanism does not consider the application of member objects

All objects in the WeakSet/WeakMap are weak references, that is, the garbage collection mechanism does not consider the reference of the WeakSet to the object, that is, if other objects no longer reference the object, the garbage collection mechanism will automatically reclaim the memory occupied by the object, regardless of the existence of the object in the WeakSet.
This is because the garbage collection mechanism judges the collection according to the reachability of the object. If the object can still be accessed, the garbage collection mechanism will not release this memory. After using this value, sometimes you forget to dereference, resulting in memory can not be released, which may lead to memory leakage. The references in the WeakSet are not included in the garbage collection mechanism, so this problem does not exist. Therefore, WeakSet is suitable for temporarily storing a group of objects and information bound to objects. As long as these objects disappear externally, their references in the WeakSet will disappear automatically.
Due to the above characteristics, the member of WeakSet is not suitable for reference because it will disappear at any time. In addition, the number of members in the WeakSet depends on whether the garbage collection mechanism is running. The number of members is likely to be different before and after the operation, and the operation time of the garbage collection mechanism is unpredictable. Therefore, ES6 stipulates that the WeakSet cannot be traversed.

Proxy

Proxy can be understood as setting up a layer of "interception" before the target object. External access to the object must first pass through this layer of interception. Therefore, it provides a mechanism to filter and rewrite external access. The original meaning of proxy is proxy. It is used here to "proxy" some operations, which can be translated as "proxy".

var proxy = new Proxy(target, handler);

  • The target parameter represents the target object to be intercepted, and the handler parameter is also an object to customize the interception behavior.
  • If the handler does not set any interception, it is equivalent to going directly to the original object.
  • Use the proxy instance proxy to access the properties of the target
  • One trick is to set the proxy object to object Proxy property, which can be called on the object object.
var object = { proxy: new Proxy(target, handler) };
  • Although the in operator is also used in the for... In loop, the has() interception has no effect on the for... In loop.
const star = {
    name: "Yoona Lim",
    age: 23,
    tel: 'star 666'
}
const agent = new Proxy(star, {
    get: function (target, key) {
        if (key === 'tel') {
            return 'rejected 110'
        } else {
            return target[key]
        }
    },
    set: function (target, key, val) {
        if (key === 'offerPrice') {
            if (val < 5000) {
                throw new ReferenceError('below standard!')
            } else {
                target[key] = val
            }
        } else {
            target[key] = val
        }
    },
    has: function (target, key) {
        console.log('be gone has intercept')
        return target.hasOwnProperty(key)
    }
})
console.log(agent.name)
console.log(agent.age)
console.log(agent.tel)
agent.cute = true
console.log(agent.cute)
agent.offerPrice = 50000
console.log(agent.offerPrice)
console.log('offerPrice' in agent)
console.log('gender' in agent)
for (let key in agent) {
    console.log('for...in Don't go has intercept', agent[key])
}

console.log(agent)

console.log(star)
agent.offerPrice = 50

Reflect

The Reflect object is designed for several purposes.
(1) Put some methods of the Object object that obviously belong to the language (such as Object.defineProperty) on the Reflect Object. At this stage, some methods are deployed on both Object and Reflect objects. In the future, new methods will only be deployed on Reflect objects. In other words, the internal methods of the language can be obtained from the Reflect Object.
(2) Modify the returned results of some Object methods to make them more reasonable. For example, Object Defineproperty (obj, name, DESC) will throw an error when the property cannot be defined, and reflect Defineproperty (obj, name, DESC) will return false.
(3) Make Object operations become function behavior. Some Object operations are imperative, such as name in obj and delete obj[name], while reflect Has (obj, name) and reflect Deleteproperty (obj, name) makes them function behavior.