Say I have an object, var obj = {}
. I want to have a handler invoked when at a later time, any field is retrieved; obj.b
.
I am aware of proxies,
var obj = {};
var proxy = new Proxy(obj, {
get(obj, prop) {
console.log('get', prop)
return Reflect.get(obj, prop);
}
})
However, this will only log when accessing properties through the proxy proxy.b
and will fail to log otherwise obj.b
.
I am also aware of using defineProperty
to put getters on all the keys of obj
. However, this won't handle new fields that are added to obj
after the getters were added.
Maybe if it's possible to re-implement proxy, then it should be possible to add the handlers to the original obj
instead of the proxy
. I've tried investigating the proxy object, but it seems to be implemented internally. new Proxy({},{})
has properties [[Handler]]
, [[Target]]
, and [[IsRevoked]]
, but I was not able to access or view these objects; proxy['[[Handler]]']
returns undefined
. I was thinking if I could just add those handler and target fields to obj
myself, instead of creating a separate proxy object, then I could make obj
act like a proxy.
I have read through MDN's docs for proxies, defineProperty, and handlers and even http://exploringjs.com/es6/ch_proxies.html. I've likewise come across multiple stack overflow posts achieving something similar but not quite complete.
Aucun commentaire:
Enregistrer un commentaire