I am trying to mimic a more class-like inheritance model using JavaScript, but I hit a problem when trying to mix this with the idea of JavaScript Proxies.
To make a long story short, in the definition of my Class type I have a function _super() with the semantics "when method X on an instance of subclass B invokes _super(), call method X on parent class A":
Class A
.X() {...}
^
|
|
Class B
.X() {..._super(); ...}
I am relying on the function.caller.name approach to get me the name of the invoking method (in our example, "X"). Then I call it on the parent class.
const Class = {
...
_super: function _super(...args) {
// Get a handle on the function in which this function is invoked:
const callerMethod = _super.caller.name;
...
},
...
};
This works correctly. The problems started when I added a Proxy object on top of my Class construct (I want to trap some method calls).
function traceMethodCalls(obj) {
const handler = {
get(target, propKey, receiver) {
const origMethod = target[propKey];
return function (...args) {
// Do stuff
};
},
};
return new Proxy(obj, handler);
}
Now, function.caller in the _super() method is the anonymous function in the Proxy handler object (obviously...), and this messes up the program flow.
My question: is there a way to circumvent this? Or think about it differently? Or do I have to give up on the *.caller.name approach altogether?