I download the csv file and parse it. and I want the resulting array to be a member of a specific object, but it ends undefined because I am not using the "this" keyword correctly.
function SimPlayer(){
this.dataset = new Array();
var client = new XMLHttpRequest();
var dset = this.dataset;
function handler(){
if(client.readyState == 4){
if(client.status == 200){
dset = client.responseText.split("\n");
for(var i=0; i<dset.length; i++){
dset[i] = dset[i].split(",");
for(var j=0; j<dset[i].length; j++){
dset[i][j] = parseInt(dset[i][j]);
}
}
console.log(dset[0]);
}
}
}
client.onreadystatechange = handler;
client.open("GET", "http://nathannifong.com/LayerCake/simdata/rec0_i0.csv");
client.send();
this.check = function(){
console.log(this.dataset[0])
}
}
Suppose I create an instance of SimPlayer and then invoke verification later (after the csv file has managed to load)
foo = new SimPlayer();
foo.check();
foo.check () calls
Uncaught TypeError: Cannot read property '0' of undefined
How can I fix my code so that in check () this .dataset will contain data from the csv file?
source
share