简体   繁体   English

如何存储 function 的参数以在内部函数中使用

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

I have the following kind of code in NodeJS (Typescript):我在 NodeJS (Typescript) 中有以下类型的代码:

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

}

and the aClass.motherFunction looks like this: aClass.motherFunction 看起来像这样:

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

}

and the otherClass.childFunction looks like this:和 otherClass.childFunction 看起来像这样:

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.关键是arg1:MyObject,它只需要在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.我想知道是否可以将参数存储在某处,例如在执行线程中,然后在 grandchildFunction 中检索它。 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.由于它是一个 Express 应用程序(实际上它是一个 Loopback 4.0),我曾考虑在原始请求中执行此操作,但我不希望最后一个 otherClass 导入请求。

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?因此,如果我理解正确,您有一个变量arg1: MyObject仅由一个 function 使用,并且 function 仅通过另一个调用,您不想通过其他函数传递arg1

If so, I would maybe set that argument as this.arg1: MyObject and call this.arg1 someOtherClass.grandChildFunction ...如果是这样,我可能会将该参数设置为this.arg1: MyObject并调用this.arg1 someOtherClass.grandChildFunction ...

So you could have所以你可以有

private grandChildFunction(arg2: any){

    otherClass.grandChildFunction(this.arg1);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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