私は、ユーザーが1つのパズルから次のパズルに進むことができるオンラインゲームを書いています。ユーザーがミスをした場合、各パズルには、ユーザーがそのパズルだけを最初から開始できる[再開]ボタンがあります。コードの構造の簡略化されたバージョンは次のとおりです。
function puzzle(generator) {
this.init = function() {
this.generator = generator;
...
this.addListeners();
}
//fires when the puzzle is solved
this.completed = function() {
window.theSequence.next();
}
this.empty = function() {
//get rid of all dom elements, all event listeners, and set all object properties to null;
}
this.addListeners = function() {
$('#startOver').click(function() {
window.thePuzzle.empty();
window.thePuzzle.init();
});
}
this.init();
}
function puzzleSequence(sequenceGenerator) {
this.init = function() {
//load the first puzzle
window.thePuzzle = new puzzle({generating json});
}
this.next = function() {
//destroy the last puzzle and create a new one
window.thePuzzle.empty();
window.thePuzzle = new puzzle({2nd generating json});
}
}
window.theSequence = new puzzleSequence({a sequence generator JSON});
私が抱えている問題は、ユーザーが2番目のパズルに進んだ場合、[最初からやり直す]をクリックすると、2番目のパズルではなく最初のパズルが読み込まれることです。少しデバッグした後、2番目のパズルのメソッドで使用された場合、何らかの理由で最初のパズルへの参照が保持されていることがわかりましたが、「window.thePuzzle」はこれと同じである必要があります--2番目のパズルを正しく参照します。
なぜ「これ」は最初のものを参照し続けるのですか?
さらにコードサンプルが必要な場合はお知らせください