initialSpeed is not being updated, it shows first as undefined and later as NaN.
the start() and calcSpeed() methods work perfectly fine when outside the class.
class Transportation {
kind: string;
speed: number;
initialSpeed: number = 0;
constructor(kind: string, speed:number) {
this.kind = kind;
this.speed = speed;
}
start() {
let begin = setInterval(this.calcSpeed, 1000);
}
calcSpeed() {
console.log("initial speed: ", this.initialSpeed);
return this.initialSpeed = this.speed + this.initialSpeed;
}
}
let car = new Transportation("car", 50);
console.log(car);
car.start();
It should show 0 and every second increase by 50. Instead, it shows undefined and every second after that as NaN.
I've tried Number() and toString() just in case but didnt work.