-5

Let's say that:

 a() {
  console.log("i am a")
}

 b() {
   c() {
    //call function a here 
  }
}

How do I call function a from function c?

4castle
  • 32,613
  • 11
  • 69
  • 106

3 Answers3

-1

functions declared like you are declaring them are hoisted to the top of the scope. a is available globally and can be called by anything inside the same global scope.

lluisrojass
  • 439
  • 1
  • 3
  • 12
-1

I have solved my problem with using an arrow function on parameter of c.

-1

The way your functions are defined shows that c is nested inside of b which effects the scope when the function executes. First, you would have to figure out a way to call function c. Since it is inside of function b, you will first need to call function b to get to function c. Accessing function c is not possible with your code.

The below code would allow you to call function c, which then calls function a.

function a() {
  console.log("i am a")
}

function b() {
   this.c = function() {
    //call function a here
    window.a();
  }
  
  return this;
}

b().c()
Allen Tellez
  • 1,198
  • 1
  • 10
  • 14