Answer the question
In order to leave comments, you need to log in
How to rewrite a method in JS so that it is called with one more argument by default?
I have a class, in my case it is a Subject in which I want to override the pipe so that, in addition to everything that is passed to it, it passes catchError to itself. There is an example on the stack https://stackblitz.com/edit/typescript-mvqpgt
Here we have an ExampleClass that has a showParams method, and I need this method to work differently and I want to change its behavior after class initialization (object creation ). I need that in addition to the arguments that this method will take, it will be called with one more argument at the end.
Answer the question
In order to leave comments, you need to log in
1. Are you sure that you want to patch the whole class and all its descendants, and not its specific instance?
2. You have a number of errors not related to ts in any way, this line: contains two errors:
a) calling inside you teach infinite recursion, you must save the old one into a separate variable and call through it;
b.) takes an array as the second argument, and you parse it in vain here.
If the answer to question 1 is "no", then something like this should be:this.showParams.apply(this, ...args);
class ExampleClass {
showParams(...args) {
console.log('I have to be calleb');
console.log(...args);
}
}
const exampleInstance = new ExampleClass();
exampleInstance.showParams('one', 'Two', 'Three', 456);
exampleInstance.showParams = (showParamsOld => function(...args){
args.push('Default Argument');
return showParamsOld.apply(this, args);
})(exampleInstance.showParams);
exampleInstance.showParams('one', 'Two', 'Three', 456);
If the answer is yes, then correct yourself. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question