0

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()
     */
  }
}

5 Answers 5

0

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();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();
  }
}

Comments

0

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

Comments

0

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()

Comments

0
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();

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.