I've encountered an issue with my class setup below. Despite most things working, the console keeps throwing an error when json.onload is triggered. The error message reads "
Uncaught TypeError: Cannot read property of 'push' of undefined
".
How can I resolve this error?
var Clazz = new function(){
this.library = [];
this.add_library = function(file){
var json = new XMLHttpRequest();
json.overrideMimeType("application/json");
json.open("GET", file, false); //needs to be synchronise
json.onload = function(){
this.library.push(JSON.parse(json.responseText));
do something...
};
json.send(null);
};
}
Clazz.add_library("path/file.json");
Solution
Simply change this.library.push(...);
to Clazz.library.push(...);