I tried to load a JSON file inside a js class. The loaded data is available ony inside the function.
class MakePage {
constructor() {
var request=new XMLHttpRequest();
request.open('GET','daten.json');
request.responseType='json';
request.send();
request.onload=function() {
this.daten=request.response;
alert(this.daten[1].Name); // this works
}
}
test(name) {
document.getElementById(name).innerHTML=this.daten[1].Name;
}
}
page=new MakePage();
page.test("xyz"); // TypeError: this.daten is undefined
How can I store the loaded data in a class member?
EDIT: Thanks for the answers. I tried self=this, bind(this) and the arrow operator, but I always get the same TypeReference error.