NodeJS - 从另一个嵌套函数内部调用嵌套函数

发布于 2025-01-15 23:46:36 字数 374 浏览 1 评论 0原文

我有两个嵌套函数,我想在第一个函数中调用第二个函数,但我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↙温凉少女 2025-01-22 23:46:36

我认为你不应该使用this。试试这个方法:

function Main(){
  const NestedA = function() {
    console.log('Hello from A')
  }

  const NestedB = function() {
    console.log('Hello from B');
    NestedA();
  }
}

I think you should not use this. Try this way:

function Main(){
  const NestedA = function() {
    console.log('Hello from A')
  }

  const NestedB = function() {
    console.log('Hello from B');
    NestedA();
  }
}
情释 2025-01-22 23:46:36

这是什么?确实,您不应该需要这个。只需将声明更改为普通函数声明,您甚至可以对像下面的 NestedC 这样的函数使用 const 赋值。

function Main(){
  function NestedA(){
    console.log('Hello from A')
  }
  const NestedC= () => {
    console.log('Hello from A')
  }

  function NestedB(){
    console.log('Hello from B')
    NestedA();
  }
}

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.

function Main(){
  function NestedA(){
    console.log('Hello from A')
  }
  const NestedC= () => {
    console.log('Hello from A')
  }

  function NestedB(){
    console.log('Hello from B')
    NestedA();
  }
}
独﹏钓一江月 2025-01-22 23:46:36

您还可以返回要调用的函数,并调用它,例如:

 function Main() {
  this.NestedA = function () {
    console.log("Hello from A");
  };

  return (this.NestedB = function () {
    console.log("Hello from B");
  });
}

Main2()(); //Hello from B

You could also return the function that you want to call, and call it such as:

 function Main() {
  this.NestedA = function () {
    console.log("Hello from A");
  };

  return (this.NestedB = function () {
    console.log("Hello from B");
  });
}

Main2()(); //Hello from B
不即不离 2025-01-22 23:46:36

无需使用 this

尝试此代码片段

function Main(){
  NestedA = function(){
    console.log('Hello from A')
  }

  NestedB = function(){
    console.log('Hello from B')
     NestedA();
  }
NestedB();

}
Main();

调用 Main()
Main() 调用 NestedB()
NestedB() 调用 NestedA()

No need to use this

Try this snippet

function Main(){
  NestedA = function(){
    console.log('Hello from A')
  }

  NestedB = function(){
    console.log('Hello from B')
     NestedA();
  }
NestedB();

}
Main();

Calling Main()
Main() call NestedB()
NestedB() call NestedA()

筑梦 2025-01-22 23:46:36
function Main(){
  var that = this;
  this.NestedA = function() {
    console.log('Hello from A')
  }

  this.NestedB = function() {
    console.log('Hello from B');
    that.NestedA();
  }
}

var m = new Main();
m.NestedB();
function Main(){
  var that = this;
  this.NestedA = function() {
    console.log('Hello from A')
  }

  this.NestedB = function() {
    console.log('Hello from B');
    that.NestedA();
  }
}

var m = new Main();
m.NestedB();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文