This is the first time I've used JS objects and I'm confused as to why this property is always undefined:
function Rotator() {
this.interval = 300;
this.image = 0;
this.images = undefined;
}
Rotator.prototype.Fetch = function(links) {
console.log("Fetch called");
this.images = links;
}
Rotator.prototype.Current = function() {
if (this.images == undefined) {
console.log("Error, images is undefined");
}
return this.images[this.image];
}
r = new Rotator;
$.getJSON("./data.php", function (data) {
r.Fetch(data.images);
});
console.log(r.Current());
The error I get is:
Uncaught TypeError: Cannot read property '0' of undefined
The JSON returned is working fine, and fetch is marked as called in the console (when logged the data is fine as well). Why is Rotator.images always undefined?
Edit: Some console.log results:
- Logging
data.images
in$.getJSON
results in correct data. - Logging
links
inFetch
results in correct data. - Logging
this.images
inFetch
results in correct data. - Logging
this.images
inCurrent
results in null.