I have created a simple Proxy example on a Map Object. I can't figure out why the Set handler is not being executed and Get is executed instead, for Sets.
const handler = {
get: (targetObj, propName, receiverProxy) => {
console.log('PROXY: From get.')
let ret = Reflect.get(targetObj, propName, receiverProxy)
if (typeof ret === 'function') {
ret = ret.bind(targetObj)
}
return ret
},
set: (targetObj, propName, propValue, receiverProxy) => {
console.log('PROXY: From set.')
return Reflect.set(targetObj, propName, propValue, receiverProxy)
},
}
const targetMap = new Map([
['iron', 'man'],
])
const targetObj = {}
const proxy = new Proxy(targetMap, handler)
console.log(proxy.set('super', 'man'))
console.log(proxy.get('super'))
get()because the proxy will be involved in accessing the "set" property of the target object (the Map instance). Theset()andget()methods of the handler are about property access and update; they have nothing to do with the methods on the Map prototype. You'll see yourset()called if you triedproxy.x = 1;or something..set()and.get()calls, see the second part of the answer.