Notes › JavaScript
The this Keyword in JavaScript
In JavaScript, this is decided by how a function is called, not where it is written (except for arrow functions).
The four rules (in order of priority)
| How the function is called | this is |
|---|---|
1. new Foo() |
the new object |
2. fn.call(obj) / apply / bind |
obj (explicit) |
3. obj.method() |
obj (implicit — whatever is left of the dot) |
4. plain fn() |
undefined in strict mode, the global object (window) otherwise |
const user = {
name: "Asha",
greet() { return `Hi, I am ${this.name}`; },
};
user.greet(); // "Hi, I am Asha" — rule 3
const g = user.greet;
g(); // this is undefined (strict) → TypeError / "Hi, I am undefined"
function Person(name) { this.name = name; }
const p = new Person("Ravi"); // rule 1 — this = the new object
call, apply and bind
function intro(city, year) {
return `${this.name} from ${city}, ${year}`;
}
const asha = { name: "Asha" };
intro.call(asha, "Jaipur", 2026); // args one by one
intro.apply(asha, ["Jaipur", 2026]); // args as an array
const bound = intro.bind(asha, "Jaipur");// returns a new function, this fixed
bound(2026);
A bound function's this can't be changed again (except by new).
Arrow functions
Arrow functions don't have their own this — they use this from the surrounding code (lexical this). call/bind can't change it.
const team = {
name: "Codify",
members: ["A", "B"],
list() {
return this.members.map((m) => `${m} of ${this.name}`); // arrow → this = team ✅
},
listBroken() {
return this.members.map(function (m) { return this.name; }); // own this → undefined ❌
},
};
Don't use arrow functions as object methods when you need this to be the object:
const obj = { name: "X", hi: () => this.name }; // this is NOT obj
Losing this in callbacks
class Timer {
constructor() { this.seconds = 0; }
start() {
setInterval(function () { this.seconds++; }, 1000); // ❌ this is not the Timer
setInterval(() => { this.seconds++; }, 1000); // ✅ arrow keeps this
}
}
button.addEventListener("click", obj.handle); // ❌ this = button
button.addEventListener("click", obj.handle.bind(obj)); // ✅
In React class components this is why handlers were bound in the constructor; function components with hooks avoid the problem.
this in classes
Class bodies are in strict mode. Methods get this from the call; class fields with arrows bind it automatically:
class Counter {
count = 0;
increment = () => { this.count++; }; // always bound to the instance
}
Output questions
const o = {
x: 10,
getX() { return this.x; },
getXArrow: () => this.x,
};
console.log(o.getX()); // 10
console.log(o.getXArrow()); // undefined (this is the outer/module this)
const f = o.getX;
console.log(f()); // undefined, or TypeError in strict mode
console.log(f.call({ x: 5 })); // 5
Interview one-liners
thisdepends on the call site; arrows take it from the enclosing scope.- Priority:
new> explicit (call/apply/bind) > implicit (obj.) > default. callandapplyinvoke immediately;bindreturns a new function.- Arrow functions can't be used as constructors and have no
arguments. - Related: JavaScript Scope and Closures.