var target = {}
var handler = {
get (target, key) {
console.info('get')
return target[key]
},
set (target, key, value) {
console.info('set')
return true
}
}
var proxy = new Proxy(target, handler)
proxy.a = 'a' //prints 'set'
console.info(proxy.a); // prints 'get'
// <- undefined
I was trying to understand the Javascript Proxy API and decided to try some code myself. I have defined an empty target to and a handler with a set and get traps. My set and get traps work perfectly fine and they're invoked everytime I set or get a property. But the last console.info statement prints an undefined. The only fix I found is to add a target[key] = value; just above the return statement in the set trap. But I'm pretty sure that's not correct. Can anyone help me figure out why I can't set a property using the proxy here? Thanks in Advance :)
Aucun commentaire:
Enregistrer un commentaire