let’s say I have the following legacy code:
function main() {
// ... setup arg
let value = method1(arg);
}
function method1(arg) {
// ... setup passing argument passedArg based on arg
method2(passedArg);
}
function method2(arg) {
// ... setup passing argument passedArg based on arg
method3(passedArg);
}
function method3(arg) {
// setup return value xxx based on arg
return xxx;
}
// and so on ...
main();
but now business change I need to call a third party api in the method3
and the returned api value will be used in method3
:
function main() {
let apiValue = ...; // call a third party api
let value = method1(arg);
}
// ...
function method3(arg, newApiValue) {
// setup return value xxx based on arg and apiValue generated in main method
return xxx;
}
for some reason, I can only call the api in the main method(), so I have to pass apiValue all the way down to method3 by modifying method1
and method2
signatures:
function method1(arg, arg2) {
// ... setup passing argument passedArg based on arg
method2(passedArg, arg2);
}
function method2(arg, arg2) {
// ... setup passing argument passedArg based on arg
method3(passedArg, arg2);
}
even though method1
and method2
don’t need api value
Is it a way to “bind” the apiValue
to method3
? I was thinking to use something like Object.setPrototypeOf()
but in the case I am not creating new objects using constructor fucntions