NodeJS - 从另一个嵌套函数内部调用嵌套函数
我有两个嵌套函数,我想在第一个函数中调用第二个函数,但我的nodejs似乎无法识别它。
function Main(){
this.NestedA = function(){
console.log('Hello from A')
}
this.NestedB = function(){
console.log('Hello from B')
/* How to call NestedA from here? */
/**
* I tried
* NestedA()
* this.NestedA()
*/
}
}
i have two nested functions, i want to call the second one inside the first one but i nodejs doesn't seem to recognize it.
function Main(){
this.NestedA = function(){
console.log('Hello from A')
}
this.NestedB = function(){
console.log('Hello from B')
/* How to call NestedA from here? */
/**
* I tried
* NestedA()
* this.NestedA()
*/
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你不应该使用
this
。试试这个方法:I think you should not use
this
. Try this way:这是什么?确实,您不应该需要
这个
。只需将声明更改为普通函数声明,您甚至可以对像下面的 NestedC 这样的函数使用 const 赋值。What is this? Really, you should not need
this
. Just change the declaration to normal function declarations, you might even use const assignment for functions like NestedC below.您还可以返回要调用的函数,并调用它,例如:
You could also return the function that you want to call, and call it such as:
无需使用
this
尝试此代码片段
调用
Main()
Main()
调用NestedB()
NestedB()
调用NestedA()
No need to use
this
Try this snippet
Calling
Main()
Main()
callNestedB()
NestedB()
callNestedA()