How to call a function from a nested function in typescript?

I want to call the func2 function from the func2 function sample function. Can someone suggest a way to achieve this?

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

Thanks at Advance

+6
source share
1 answer

Use the this with the arrow function as follows:

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

The trick uses the arrow function because the arrow function changes the definition of this as an instance of class instead of the current scope. You can read more here.

+13
source

Source: https://habr.com/ru/post/1014096/


All Articles