← Week 4 — 20 / 24 →

ES6 New Features: Arrow Functions

Arrow functions do not have their own this context, so they are useful for callbacks and event handlers.

class Timer {
  constructor() { this.seconds = 0; }
  start() {
    setInterval(function () { this.seconds++; }, 1000); // 'this' is not the Timer: NaN or a TypeError
    setInterval(() => { this.seconds++; }, 1000);        // works
  }
}