Arrow Functions
Arrow functions provide concise syntax and lexically bind this from the surrounding scope — no bind(this) needed.
Arrow functions provide concise syntax and lexically bind this from the surrounding scope — no bind(this) needed.
const add = (a, b) => a + b;
const double = n => n * 2;
const obj = () => ({ key: "value" }); // wrap in () to return object
const nums = [1, 2, 3].map(n => n * 2); // [2, 4, 6]
// Arrow keeps outer "this"
this.timer = setInterval(() => this.tick(), 1000);
Do not use arrow functions as object methods — they inherit outer this, not the object.
More in JavaScript