【发布时间】:2021-10-13 21:12:37
【问题描述】:
所以,我遇到了一个问题,我经常遇到不确定的错误,(在浏览器中)并且似乎无法弄清楚为什么/如何解决它。从我收集到的信息来看,关键字“this”在 js 中是一个完全令人困惑的混乱,而且在 ts 中也是如此。
这是我的问题:
export class XY
{
property: boolean;
//other proprs
constructor()
{this.property = false;}
setproperty(e) //this is an onclick event
{
this.property = true;
//my problem is that this.property doesn't refer to class member variable "property"
}
}
即使我尝试类似 const boundGet = XY.bind.(xy),它仍然未定义且“无法访问”
如果不使用关键字“this”,我应该如何引用成员变量属性而不使其成为 const??
【问题讨论】:
-
我会阅读并更新,谢谢!
-
这里的问题是您没有正确初始化构造函数中的实例成员。应该是:
constructor() { this.property = false; } -
进行了编辑。我做我的代码,只是不正确地复制它,堆栈溢出大声笑。
标签: angular typescript ecmascript-2017