简体   繁体   中英

How do I call a function from within a nested function in typescript?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM