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
You can call the same function from within itself in TypeScript. This is called recursion.
To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.
Calling a function from within itself is called recursion and the simple answer is, yes.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With