简体   繁体   中英

How to store the argument of a function to use in inner functions

I have the following kind of code in NodeJS (Typescript):

private grandMotherFunction(arg1: MyObject, arg2: any){
...
aClass.motherFunction(arg1)
...

}

and the aClass.motherFunction looks like this:

private motherFunction(arg1: MyObject){
...
otherClass.childFunction(arg1,otherArgument)
...

}

and the otherClass.childFunction looks like this:

private childFunction(arg1: MyObject, arg2: any){
...
someOtherClass.grandChildFunction(arg1, somethingMore)
...

}

and the code follows like this, passing the same argument to all the functions in different Classes. The point is that the arg1:MyObject, it is only needed in the granchildFunction. I would like to know if it would be possibe to store the argument somewhere, like in the executing Thread and then in the grandchildFunction retrieve it. As it is a Express App (in fact it is a Loopback 4.0), I have thought to do it in the originating request, but i don't want the last otherClass to import the Request.

So, if I understand correctly, you have a variable arg1: MyObject that is only being used by one function, and that function is only invoked through another one and you don't want to have to pass arg1 through the other functions?

If so, I would maybe set that argument as this.arg1: MyObject and call this.arg1 someOtherClass.grandChildFunction ...

So you could have

private grandChildFunction(arg2: any){

    otherClass.grandChildFunction(this.arg1);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM