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()
*/
}
}
CodePudding user response:
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();
}
}
CodePudding user response:
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();
}
}
CodePudding user response:
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
CodePudding user response:
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()