Given a class like:
class A {}
Is it possible to modify in any way so that I can proxy when methods are subclassed like so:
// proxy the `A` object in some way
const handler = {
get(target, property) {
console.log("getting", arguments);
return target[property];
},
set(target, property, value) {
console.log("setting", arguments);
return target[property] = value;
},
};
const A_proxied = new Proxy(A, handler);
I want the message setting hello (or similar) to be logged in the following case.
class B extends A_proxied {
hello() {
return "world";
}
}
The above code just logs getting prototype and nothing else. Any ideas?
Don't use a
classthat should be subclassed for this. Inheritance is the wrong solution to your problem, and proxies won't help either.If you want someone to execute code when defining a class, give them a function to call:
You can declare it as