如何在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
中定义新函数