从函数的回调中访问类的私有成员

时间:2017-11-25 11:24:46

标签: javascript typescript

这是我的代码

class MyClass {
  private ref: any;
  private ourprogress: any = 0;
  onUploadAll() {
    ref.api(somedata).then(function (snapshot) {
         this.ourprogress++;//Not working
    });
  }
}
无论什么

,我们的进步都保持不变

1 个答案:

答案 0 :(得分:0)

这有效:

class MyClass {
  private ref: any;
  private ourprogress: any = 0;
  onUploadAll() {
    ref.api(somedata).then((function (snapshot) {
         this.ourprogress++;//Now works
      }).bind(this));
  }
}