this — *JavaScript's Mystery*
this — *JavaScript's Mystery*
🎯 What you'll be able to do after this lesson
After finishing this lesson, you'll be able to confidently do the following 3 things.
- ▸✅ Understand how a regular function's
thischanges depending on the call context - ▸✅ Understand the difference in
thisbinding with arrow functions - ▸✅ Know when to use
call,apply, andbind
Keep the learning objectives as a checklist, and close the lesson once you can answer all of them.
this is determined in *4 ways*
Core in One Line
In JS, this is determined in 4 ways based on how the function is called. It's the biggest difference from other languages. Memorize it once, remember it for life.
1. Default Call — global or undefined
A regular function call has no call context → the global object or undefined.
2. Method Call — the object before the dot
Even the same function has a different this depending on how it is called.
3. new Call — a new instance
When called with the new keyword, a new object is created and that object becomes this.
4. Explicit Binding — call · apply · bind
Arrow Function — has no this
Arrow functions have no this of their own — they use the this of the outer scope. The most common use case:
In React
In functional components (the modern standard), this is not used at all. Hooks replace it.
Decision Order (no need to memorize, just reference)
1. new? → new object
2. Explicit call · apply · bind? → that object
3. obj.method() dot notation? → that object
4. Otherwise → global / undefined (strict)
Summary
- ▸this is determined by how the function is called
- ▸Method call → object before the dot
- ▸
newcall → new instance - ▸Explicitly overridable with
bind · call · apply - ▸Arrow functions have no this of their own — the safest choice
⚡ Try it yourself — 4 ways to call this
🤖 Try asking AI like this
Once you understand the concepts in this lesson, you can give AI specific instructions. Instead of a vague 'fix this', you make requests with vocabulary — that's where token savings begin.
- ▸"Fix this callback's this binding issue using an arrow function or bind"
- ▸"Stabilize this class method with explicit .bind(this)"
- ▸"Tell me how this code's this behavior changes when strict mode is enabled"
Why This Reduces Tokens
Without knowing the concepts, you have to follow up with 'What does that mean?' after receiving an AI response. That follow-up question is what consumes tokens. Learn the concept once, and the conversation ends in one exchange.