2011-12-19 72 views
0

我正在創建一個應該可以同時運行幾個遊戲的內存遊戲。如果您重新啓動遊戲或運行新遊戲,應該可以存儲應保留的最高分數(高分)。從不同的實例中訪問一個值

現在來解決問題。如果我將變量「bestScore」存儲爲「this.bestScore」,它將指向特定遊戲,這意味着當我重新啓動遊戲時它將被重置。我曾嘗試使用「var」代替,但是當我嘗試使用「DESKTOP.MemoryApp.bestScore」訪問它時(請參閱下面的代碼),它是未定義的。

那麼,存儲這個值的最好方法是什麼,以便它可以在所有遊戲中使用?

DESKTOP.MemoryApp = function(){ 

this.score = 0; 
this.bestScore = 0; 
var bestScore = 0; 

} 

DESKTOP.MemoryApp.prototype.wonGame = function(){ 

// code... 

console.log(this.bestScore) // <-- points to a specific game 
console.log(DESKTOP.MemoryApp.bestScore // <-- undefined 
console.log(bestScore) <-- bestScore is not defined 

}

回答

1

存儲它與您試圖訪問它在第二種情況:

// this will work from the "constructor" function as well 
DESKTOP.MemoryApp.bestScore = 100; 
console.log(DESKTOP.MemoryApp.bestScore); // of course, 100 
+0

啊,當然!謝謝! – holyredbeard 2011-12-19 21:18:41

相關問題