I am working with JavaScript Proxy and do not understand why the target and the receiver are different in both the get() and set() traps. They appear to be the same (based on console logging). The mozilla docs say that the receiver is either the proxy or the object that inherits from the proxy; in this case, it's clearly not the proxy. (FWIW, this
is the proxy itself in the get() and set() traps.)
This code:
const s = 'this is a string';
const o = {a: s};
const p = new Proxy(o, {
get(target, prop, recv) {
console.log('get: target', target, 'recv', recv, 'same', target === recv);
return target[prop];
},
set(target, prop, value, recv) {
console.log('set: target', target, 'recv', recv);
Reflect.set(target, prop, value, recv);
//target[prop] = value;
console.log('set: target', target, 'recv', recv, 'same', target === recv);
return value;
},
});
let z = p.a;
p.a = 'also a string';
z = p.a;
results in this output:
get: target { a: 'this is a string' } recv { a: 'this is a string' } same false
set: target { a: 'this is a string' } recv { a: 'this is a string' }
set: target { a: 'also a string' } recv { a: 'also a string' } same false
get: target { a: 'also a string' } recv { a: 'also a string' } same false