内部匿名函数中引用外部命名空间函数

时间:2019-03-29 18:45:45

标签: typescript reference namespaces

我需要从内部函数访问打字稿类中的某些方法。内部函数显然具有不同的命名空间,因此仅使用this.outerMethod()不起作用。

因此,我使用了一个变量来引用外部的“ this”:

let ref = this;
...
function innerFunction(){
   ref.outerMethod();
}

但是它只是说'self is undefined'。

最重要的是,有时我需要调用的外部方法是指类属性,这些属性也位于外部名称空间中。

要成功引用内部函数中的外部方法,我该怎么做?

1 个答案:

答案 0 :(得分:1)

改为使用以下语法:

let innerFunction = ()=>
{
    ref.outerMethod();
}

相关问题