When Arrow Functions Break Methods
You'll learn: Why arrow functions can cause unexpected bugs when used as object methods.
The Problem with Arrow Methods
Arrow functions don't have their own this — they inherit it from wherever they're defined, not where they're called. This is usually helpful, but it breaks a fundamental pattern in objects: methods that need to access their own object's properties.
When you define an object method as an arrow function, this won't point to the object itself. Instead, it captures this from the outer scope (often the global scope or undefined in strict mode).
Why Regular Methods Work
const user = {
name: 'Alice',
greet: function() {
console.log(`Hello, I'm ${this.name}`);
}
};
user.greet(); // "Hello, I'm Alice" ✅
Here, this refers to user because that's the object calling the method.
Where Arrow Functions Break
const user = {
name: 'Alice',
greet: () => {
console.log(`Hello, I'm ${this.name}`);
}
};
user.greet(); // "Hello, I'm undefined" ❌
The arrow function locked in this from outside the object. Since there's no name in that outer scope, you get undefined.
The Rule of Thumb
Use regular functions for object methods when you need this to refer to the object. Save arrow functions for callbacks, nested functions, or situations where you want to preserve an outer this.
Key Takeaway: Arrow functions make terrible object methods because they can't access the object through this — use regular function syntax for methods instead.