2011-12-19 21 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

相关问题