I'm using JavaScript's Proxy
object to create a get()
handler to trap property access to an instance of a class.
I would like to have different behavior depending on whether the Proxy is being called by methods within the class or by the proxy itself.
Is that possible?
EDIT: Some example code that hopefully explains what I'm trying to do:
class Something {
constructor() {
this.prop1 = 'val1';
this.prop2 = 'val2';
}
getProp() {
return this.prop1;
}
}
const instance = new Something();
const proxiedObject = new Proxy(instance, {
get: function(target, property, receiver) {
if (WHATEVER_IS_ACCESSING_THIS_IS_NOT_AN_INSTANCE_METHOD) {
return false;
}
else {
return target[property];
}
}
});
console.log(proxiedInstance.getProp()); // 'val1'
console.log(proxiedInstance.prop1); // 'false'