我在打字稿中的'this'關鍵字有問題。正如你在下面看到的,我想從一些'內部'函數調用method1,例如FileReader.onloadend方法。 Hovewer,'this'引用FileReader,而不是foo類。我怎樣才能改變我的代碼來完成這項工作?打字稿中的這個關鍵字並不涉及類
export class foo {
constructor() {
this.method2();
}
public method1() {
console.log('method1 called'); // this never happens
}
public method2() {
let reader: FileReader = new FileReader();
reader.onloadend = function(e) {
console.log(this) //it prints FileReader object
this.method1(); //I want this to be refered to class foo
}
}
}