对此感到困惑 & getcontext js/ts 以及如何解决它

Confused about this & getcontext js/ts and how to get around it

提问人:Levente 提问时间:8/10/2021 最后编辑:Levente 更新时间:8/10/2021 访问量:29

问:

所以,我有一个问题,我经常遇到未被发现的错误,(在浏览器中),似乎无法弄清楚为什么/如何解决它。据我所知,关键字“this”在 js 中是一个完全令人困惑的混乱,在 ts 中也是如此。

这是我的problme:

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”,我应该如何在不使其常量的情况下引用成员变量属性?

Angular 打字稿 ECMAScript-2017

评论

0赞 Andreas 8/10/2021
“this”关键字是如何工作的?
1赞 Andreas 8/10/2021
这回答了你的问题吗?如何在回调中访问正确的“this”
0赞 Levente 8/10/2021
我会阅读并更新,谢谢!
0赞 lbsn 8/10/2021
这里的问题是您没有在构造函数中正确初始化实例成员。应该是:constructor() { this.property = false; }
0赞 Levente 8/10/2021
进行了编辑。我做我的代码,只是复制不当,以堆栈溢出哈哈。

答:

0赞 Levente 8/10/2021 #1

因此,事实证明,问题出在Devextreme的事件绑定上。 如果你想覆盖他们的数据网格控件 “edit” buttons 事件,你必须像这样传递事件 [onClick]=“yourfunc” 现在这显然不是标准的 (event)=“blabla”。 无论在后台发生什么,都会扰乱“this”的上下文。 通过使用不同的事件来解决问题。