Why you’d want to use arrow functions (next to them being shorter to type):
Arrow functions will maintain the
this
value of the enclosing context
Yes, this will work just fine:
function Wilto() {
this.age = 32;
setInterval(() => {
this.age++;
console.log( "I am now " + this.age + " years old");
}, 3000 );
}
Whereas we used to do stuff like this:
function Wilto() {
var self = this;
self.age = 32;
setInterval( function constantBirthdays() {
self.age++;
console.log( "I am now " + self.age + " years old");
}, 3000 );
}