I’m creating a memory game that should be possible to run as several games at the same time. It should be possible to store the best score (highscore) that should remain if you restart the game or run a new game.
Now to the problem. If I store the variable “bestScore” as “this.bestScore” it will point at a specific game, which means it will be reset when I restart the game. I have tried used “var” instead, but when I try to access it with “DESKTOP.MemoryApp.bestScore” (see the code below) it is undefined.
So, what is the best way to store this value so it will be accesible in all the games?
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
}
Store it as you are trying to access it in the second case: