current position:Home>Trigger of set and definpropety in proxy instance

Trigger of set and definpropety in proxy instance

2021-08-27 05:41:36 Sometimes it rains

In the of Ruan Yifeng great God ES6 The tutorial mentioned :

Proxy.set The interception uses Reflect.set, And it came in receiver, Cause to trigger Proxy.defineProperty Intercept . This is because Proxy.set Of receiver Parameters always point to the current  Proxy example ( That is to say obj), and Reflect.set Once it comes in receiver, The attribute will be assigned to receiver above ( namely obj), Cause to trigger defineProperty Intercept . If Reflect.set There was no introduction receiver, Then it won't trigger defineProperty Intercept .

At this time, if in set In the interceptor , There is no call Reflect.set Method , Or no incoming receiver Words , It won't trigger defineProperty Intercept . Besides , If you are setting proxy The interceptor object of the instance , No settings set Interceptor , Just settings defineProperty When interceptor , adopt proxy Instance when setting the properties of the target object , It will trigger defineProperty Interceptor :

var handler = {
  defineProperty (target, key, descriptor) {
    console.log('start define')
    return false;
  }
};
var target = {};
var proxy = new Proxy(target, handler);
proxy.foo = 'bar'// 'start define'
 Copy code 

Read at the same time proxy.foo when , return undefined. This indicates that... Is not set in the interceptor object set When interceptor , adopt proxy Object to set the property value , It will pass the already set defineProperty Interceptor , Assign a value to the target attribute . In the interceptor object, only set Interceptor , Not set defineProperty When interceptor , adopt Object.defineProperty Method to set the property value , It doesn't trigger set Interceptor . This shows that through object.propName Assign a value to an attribute in the same way , covers set Interceptors and defineProperty There are two types of interceptors , In other words, it covers two methods of object attribute assignment ; and Object.defineProperty Method to assign a value to an attribute , It's not going to involve object.propName/object[propName] This kind of assignment .

copyright notice
author[Sometimes it rains],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827054133510e.html

Random recommended