Javascript 从不同实例访问值
我正在创建一个记忆游戏,可以同时运行多个游戏。如果重新启动游戏或运行新游戏,应该可以存储应保留的最佳分数(highscore) 现在来谈谈问题。如果我将变量“bestScore”存储为“this.bestScore”,它将指向一个特定的游戏,这意味着当我重新启动游戏时它将被重置。我尝试过使用“var”,但是当我尝试使用“DESKTOP.MemoryApp.bestScore”(见下面的代码)访问它时,它是未定义的 那么,存储此值以便在所有游戏中都可以访问的最佳方法是什么Javascript 从不同实例访问值,javascript,variables,object,Javascript,Variables,Object,我正在创建一个记忆游戏,可以同时运行多个游戏。如果重新启动游戏或运行新游戏,应该可以存储应保留的最佳分数(highscore) 现在来谈谈问题。如果我将变量“bestScore”存储为“this.bestScore”,它将指向一个特定的游戏,这意味着当我重新启动游戏时它将被重置。我尝试过使用“var”,但是当我尝试使用“DESKTOP.MemoryApp.bestScore”(见下面的代码)访问它时,它是未定义的 那么,存储此值以便在所有游戏中都可以访问的最佳方法是什么 DESKTOP.Memo
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
DESKTOP.MemoryApp=function(){
这个分数=0;
此项得分为0;
var得分=0;
}
DESKTOP.MemoryApp.prototype.wonGame=函数(){
//代码。。。
console.log(this.bestScore)/在第二种情况下,当您试图访问它时存储它:
// this will work from the "constructor" function as well
DESKTOP.MemoryApp.bestScore = 100;
console.log(DESKTOP.MemoryApp.bestScore); // of course, 100