👆 this Keyword
jsintermediate

👆this Keyword

this refers to the object executing the current function, determined at call-time. Arrow functions inherit this lexically — they cannot be rebound.

Memory Trick

this is like a pronoun — it means different things depending on who's speaking (calling the function).

Log in to track your mastery & progress

1️⃣ Rules for this binding

Contextthis value
Global scopewindow (browser) / undefined (strict)
Object methodThe object
Regular function callwindow or undefined (strict)
Arrow functionInherited from enclosing scope
call / apply / bindExplicitly set
Constructor (new)Newly created object

2️⃣ Common Trap — Context Loss

js
const obj = { name: 'Alice', greet() { console.log(this.name); } }; obj.greet(); // 'Alice' ✅ const fn = obj.greet; fn(); // undefined ❌ — this is now window/undefined! // Fix 1: bind const bound = obj.greet.bind(obj); bound(); // 'Alice' ✅ // Fix 2: arrow function const obj2 = { name: 'Alice', greet: () => console.log(this.name) // ❌ lexically inherits outer this };

3️⃣ Arrow Functions vs bind()

Arrow Functionbind()
HowLexically inherits thisExplicitly sets this
Rebindable❌ Never✅ Creates new function
Use caseCallbacks, short methodsLegacy code, class methods
js
// Arrow: captured at creation const timer = { seconds: 0, start() { setInterval(() => { this.seconds++; // this = timer object ✅ }, 1000); } };

Was this helpful?

Test your Knowledge

2 questions to cement what you just learned.