Use regular functions for object methods so this is the object. Use arrow functions for callbacks inside methods or constructors so this is preserved from the enclosing scope.
this in Arrow Functions vs Regular Functions
When should you use a regular function vs an arrow function for this?
The answer
More FAQs in this in Arrow Functions vs Regular Functions
Regular functions get this at call time based on how they are invoked. Arrow functions do not have their own this; they inherit it from the enclosing lexical scope at definition time.
Because an arrow function's this is the enclosing scope's this, not the object. So obj.greet = () => this.name gives undefined. Use regular functions for methods so this is the object.
Because arrows inherit this from their lexical scope. An arrow inside a method or constructor captures this from that scope, so it still refers to the instance inside the callback.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.