Full typescript and mvvm implementation #3
@ -1,15 +1,14 @@
|
|||||||
export class Stats {
|
export class Stats {
|
||||||
#score;
|
#score: number;
|
||||||
#highScore;
|
#highScore: number;
|
||||||
#highScoreStorageKey;
|
#highScoreStorageKey: string;
|
||||||
|
|
||||||
constructor(highScoreStorageKey) {
|
constructor(highScoreStorageKey) {
|
||||||
// used as key for localStorage
|
// used as key for localStorage
|
||||||
|
|
||||||
this.#highScoreStorageKey = highScoreStorageKey;
|
this.#highScoreStorageKey = highScoreStorageKey;
|
||||||
|
|
||||||
this.#score = 0;
|
this.#score = 0;
|
||||||
this.#highScore = localStorage.getItem(this.#highScoreStorageKey) ?? 0;
|
this.#highScore = Number(localStorage.getItem(this.#highScoreStorageKey)) ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementScore(value = 1) {
|
incrementScore(value = 1) {
|
||||||
@ -17,19 +16,19 @@ export class Stats {
|
|||||||
|
|
||||||
if (this.#highScore < this.#score) {
|
if (this.#highScore < this.#score) {
|
||||||
this.#highScore = this.#score;
|
this.#highScore = this.#score;
|
||||||
localStorage.setItem(this.#highScoreStorageKey, this.#highScore);
|
localStorage.setItem(this.#highScoreStorageKey, this.#highScore.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetScore() {
|
resetScore(): void {
|
||||||
this.#score = 0;
|
this.#score = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
get highScore() {
|
get highScore(): number {
|
||||||
return this.#highScore;
|
return this.#highScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
get score() {
|
get score(): number {
|
||||||
return this.#score;
|
return this.#score;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user