I have a es6 class with more then 100 functions.
I added a new function which I have to run each time any other function is called.
I found a lot of solutions but no one solved my problem...
I know I can add this call to each function but then I'll end with code repeat and a lot of work for all the functions.
That is that I've done so far:
return class Test {
constructor () {}
addedFN() {
console.log('FN');
}
A() {
this.addedFN();
console.log('A');
}
B() {
this.addedFN();
console.log('B');
}
C() {
this.addedFN();
console.log('C');
}
...
}
As we can see in this approach I have a lot of code duplication which I want to avoid...
Also, the solution to call the new function first and then inside to call the next function won't work, because all the functions are used in a lot of places in the application.
I tried also to run the following code in the constructor, but that is a bad solution as my class is called a lot of times and then this code will run each time:
for(const key of Object.getOwnPropertyNames(Test.prototype)) {
const old = Test.prototype[key];
Test.prototype[key] = function(...args) {
addedFN(...args);
old.call(this, ...args);
};
}