我想从函数 func1
的示例函数中调用函数 func2
。有人可以建议实现该目标的方法吗?
class A
{
public func1()
{
let sample = function()
{
//call func2... but how?
}
}
public func2()
{
}
}
提前致谢
最佳答案
将 this
关键字与 arrow
函数符号一起使用,如下所示:
class A
{
public func1()
{
let sample = () =>
{
this.func2();
}
}
public func2()
{
}
}
技巧是使用arrow
函数,因为arrow
函数将this
的定义更改为 的实例class
而不是当前范围。您可以阅读更多 here
关于javascript - 如何从 typescript 中的嵌套函数中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41656047/