I'm trying to get my head around various javascript concepts and one thing I'm failing to understand is why this works:
var counterObject = {
counter: 0,
start: function () {
this.i++;
console.log(this.i);
}
};
setInterval(() => counterObject.start(), 1000);
Yet, when I try and make this a recursive function, I cannot get access to the counter variable:
var counterObject = {
counter: 0,
start: function () {
setInterval(function() {
this.i++;
console.log(this.i);
this.start;
}, 1000)
}
};
counterObject.start();
This will always return NaN, and i can't seem to understand why? Just learning so go easy on my guys ;)