I have a stopwatch method that has several properties, I want no one to be able to access them from outside the class, I made them private, but it is still possible to access them and their value can be changed. what do I do?
class StopWatch {
private duration: number = 0;
private status: string = 'stopped';
private currentTime: number = 0;
start () {
if (this.status == 'started') {
throw new Error("already started!");
}
this.currentTime = Date.now();
this.status = "started";
}
stop() {
if (this.status == "stopped") {
throw new Error("already stopped!");
}
this.duration = Date.now() - this.currentTime + this.duration;
this.status = "stopped";
console.log((this.duration / 1000).toFixed(1));
}
reset() {
if (this.status == 'started') {
this.stop();
}
this.duration = 0;
}
}
const obj = new StopWatch();