如何在typescript函数中设置公共变量?

如何在typescript函数中设置公共变量?,typescript,Typescript,我正在努力在另一个函数中设置公共变量。我没能挺过去 export class MyClass{ myVar = false; myFunction(){ Module.anotherFunction(){ this.myVar = true; } } } 如何从另一个调用this.myFunction()的函数内部将myVar设置为true?如果无法使用方法语法将函数分配给另一个对象,可以执行以下操作: expo

我正在努力在另一个函数中设置公共变量。我没能挺过去

export class MyClass{
    myVar = false;

    myFunction(){
        Module.anotherFunction(){
            this.myVar = true;
        }
    }
}

如何从另一个调用this.myFunction()的函数内部将myVar设置为true?

如果无法使用方法语法将函数分配给另一个对象,可以执行以下操作:

export class MyClass{
    myVar = false;

    myFunction(){
        Module.anotherFunction = () => {
            this.myVar = true;
        }
    }
}

注意使用了箭头函数,这将把
this
绑定到
MyClass

不清楚您想做什么。您不能像以前那样在
myFunction
中定义新函数