![](/img/trans.png)
[英]Use compose with several functions and inner function with param
[英]How to store the argument of a function to use in inner functions
我在 NodeJS (Typescript) 中有以下类型的代码:
private grandMotherFunction(arg1: MyObject, arg2: any){
...
aClass.motherFunction(arg1)
...
}
aClass.motherFunction 看起来像这样:
private motherFunction(arg1: MyObject){
...
otherClass.childFunction(arg1,otherArgument)
...
}
和 otherClass.childFunction 看起来像这样:
private childFunction(arg1: MyObject, arg2: any){
...
someOtherClass.grandChildFunction(arg1, somethingMore)
...
}
并且代码如下所示,将相同的参数传递给不同类中的所有函数。 关键是arg1:MyObject,它只需要在granchildFunction中。 我想知道是否可以将参数存储在某处,例如在执行线程中,然后在 grandchildFunction 中检索它。 由于它是一个 Express 应用程序(实际上它是一个 Loopback 4.0),我曾考虑在原始请求中执行此操作,但我不希望最后一个 otherClass 导入请求。
因此,如果我理解正确,您有一个变量arg1: MyObject
仅由一个 function 使用,并且 function 仅通过另一个调用,您不想通过其他函数传递arg1
?
如果是这样,我可能会将该参数设置为this.arg1: MyObject
并调用this.arg1
someOtherClass.grandChildFunction
...
所以你可以有
private grandChildFunction(arg2: any){
otherClass.grandChildFunction(this.arg1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.