I am working on a code that involves using a function to alter existing functions and return a reference to a new function. I aim to apply this function to specific methods within a class. The current state of my code is as follows:
function modifyMethod(func) {
return function() {
console.log('working');
return func.apply(this, arguments);
};
}
function modifyClassMethods(ClassName, methodArray) {
// Implementation pending
return ClassName;
}
class Temp {
hi() {
console.log("hi method");
}
}
Temp = modifyClassMethods(Temp, ["hi"]);
const temp = new Temp();
// The expected output is
//
// working
// hi method
temp.hi();
When attempting to invoke the modifyMethod
with Temp.hi
, the func
parameter appears to be undefined. Even if an object is created and then the method is modified, the changes will only affect that particular object's method, not all objects within the same class.
It should be noted that this example serves as just a demonstration. My goal is to extend this modification to the methods of various classes. Therefore, it is not possible to generalize method names either. Any suggestions or code snippets for the modifyClassMethods
would be highly appreciated.