Lexical 'this' vs Dynamic 'this'
You'll learn the fundamental difference between how arrow functions and regular functions determine what this means.
The Core Distinction
Regular functions use dynamic binding for this — the value is determined by how the function is called (the call-site). Arrow functions use lexical binding — the value is determined by where the function is written (the surrounding scope), captured at definition time.
Think of it like addresses: a regular function asks "Who called me?" every time it runs (dynamic). An arrow function asks "Where was I born?" once and remembers forever (lexical).
Dynamic 'this' in Regular Functions
const obj = {
name: 'Dynamic',
greet: function() {
console.log(this.name);
}
};
obj.greet(); // "Dynamic" — 'this' determined at call-time
const extracted = obj.greet;
extracted(); // undefined (or error in strict mode) — different call-site!
The same function body produces different this values based on how it's invoked.
Lexical 'this' in Arrow Functions
const obj = {
name: 'Lexical',
greet: function() {
const inner = () => {
console.log(this.name);
};
inner();
}
};
obj.greet(); // "Lexical" — arrow captures 'this' from greet()
const extracted = obj.greet;
extracted(); // Still uses the outer context where arrow was defined
The arrow function looks up one level and captures whatever this means in greet(), regardless of how inner() is later called.
Why This Matters
Use regular functions when you want dynamic behavior (methods that adapt to their caller). Use arrow functions when you need predictable this that won't change (callbacks, nested helpers).
Key Takeaway: Regular functions bind this dynamically based on call-site; arrow functions capture this lexically from their surrounding scope at definition time, making them immune to how they're called.