0
function myCar() {
this.fuel = 0;
this.reFuel = function() {
    console.log(this)
    setTimeout(function(){
        console.log(this)
        this.fuel += 100
        console.log('Refueld! current fuel is ' + this.fuel)
    }, 1000)
}

}

const Bugatti = new myCar() Bugatti.reFuel(); //Refueld! current fuel is NaN

ShirinR
  • 7
  • 1
  • 1
    The value of `this` is determined at call time. When a function is scheduled for later execution with `setTimeout` it's not called with the same context as `reFuel`. You can use an arrow function to preserve the value of `this`. – VLAZ Nov 22 '20 at 20:56

0 Answers0