JS オブジェクトを使用したのはこれが初めてで、このプロパティが常に未定義である理由について混乱しています。
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());
私が得るエラーは次のとおりです。
キャッチされていない TypeError: 未定義のプロパティ '0' を読み取ることができません
返された JSON は正常に機能しており、フェッチはコンソールで呼び出されたものとしてマークされています (ログに記録されたデータも問題ありません)。Rotator.images が常に未定義なのはなぜですか?
編集: console.log の結果:
- ログイン
data.images
する$.getJSON
と正しいデータが得られます。 - ログイン
links
するFetch
と正しいデータが得られます。 - ログイン
this.images
するFetch
と正しいデータが得られます。 - ログイン
this.images
するCurrent
と null になります。