19

I want to call the function func2 from within sample function of function func1. Can someone suggest a way to achieve that?.

class A
{
   public func1()
   {
     let sample = function()
                  {
                    //call func2... but how?
                  }
   }
   public func2()
   {

   }
 }

Thanks in Advance

user3107338
  • 312
  • 1
  • 2
  • 11

1 Answers1

44

Use the this keyword with the arrow function notation like this:

class A
{
   public func1()
   {
      let sample = () => 
      {
         this.func2();
      }
   }
   public func2()
   {

   }
 }

The trick is using the arrow function, because the arrow function changes the definition of this to be the instance of the class instead of the current scope.You can read more here