`this`
this is a value passed into a function every time it is called. In most JavaScript functions it is decided by how the function is called, not by where it was written.
Detailed Explanation
Coming from Java or Python, you expect this to mean "the current object". In JavaScript that is only sometimes true. For ordinary functions, this is an implicit extra argument, and the call site chooses it. Move the same function to a different call site and this changes.
Five rules decide the value. Check them in this order.
1. new binding. new Person() creates a fresh object and sets this to it inside the constructor. This wins over everything else.
2. Explicit binding. fn.call(obj), fn.apply(obj) and fn.bind(obj) set this to obj by hand. bind returns a permanently bound copy.
3. Implicit binding. user.greet() sets this to user. The rule is the object immediately to the left of the dot at the moment of the call. Nothing about where greet was defined matters.
4. Default binding. A plain call like greet() has no receiver. In strict mode this is undefined. In sloppy (non-strict) mode it falls back to globalThis — window in a browser, the global object in Node. Since ES modules and class bodies are always strict, modern code usually sees undefined.
5. Arrow functions. An arrow function has no this of its own. It reads this from the enclosing scope at the time it was written, lexically, and no call site can change it. call, apply and bind cannot override it either.
The single biggest source of this bugs is the detached method. Writing const greet = user.greet copies the function out of the object. The dot is gone, so rule 3 no longer applies and rule 4 takes over. The same thing happens when you pass user.greet as a callback to setTimeout, addEventListener, or array.map. The fix is user.greet.bind(user) or a wrapper arrow () => user.greet().
Class methods are a common trap because class bodies are strict mode. A detached class method gets this === undefined, so the first property access throws a TypeError instead of quietly reading a global. Many codebases avoid this entirely by declaring handlers as class fields with arrow functions, which capture this at construction time.
Two more call sites worth knowing. In a DOM event handler registered with addEventListener, this is the element the listener is attached to — unless you used an arrow function, in which case it is whatever the surrounding scope had. At the top level of an ES module, this is undefined, not the global object.
The cost of arrow-function fields is memory: each instance gets its own function object rather than sharing one from the prototype. For a handful of components that is irrelevant. For tens of thousands of objects it is measurable.
Code Examples
"use strict";
const user = {
name: "Ada",
greet() {
return `Hi, ${this.name}`;
},
};
console.log(user.greet()); // => Hi, Ada (implicit: object left of the dot)
const detached = user.greet;
// console.log(detached()); // TypeError: this is undefined in strict mode
const bound = user.greet.bind(user); // explicit binding, permanent
console.log(bound()); // => Hi, Ada
console.log(user.greet.call({ name: "Grace" })); // => Hi, GraceArrow functions inherit `this`; ordinary functions do not.
// Arrow functions inherit `this`; ordinary functions do not.
const timer = {
label: "upload",
startBroken() {
setTimeout(function () {
// Ordinary function, called by the timer with no receiver.
console.log(this.label); // => undefined
}, 0);
},
startFixed() {
setTimeout(() => {
// Arrow: `this` is still `timer`, captured lexically.
console.log(this.label); // => upload
}, 0);
},
};
timer.startBroken();
timer.startFixed();Common Gotchas
Detached methods throw in strict mode
`const f = obj.method; f()` leaves `this` as `undefined`, so reading any property raises a `TypeError`. Class bodies and ES modules are always strict.
Arrow functions cannot be rebound
`call`, `apply` and `bind` silently do nothing to an arrow's `this`. Never write an object method as an arrow if it needs the object.
Sloppy mode hides the bug
in a non-strict script, default binding gives `globalThis` instead of `undefined`, so the code reads garbage rather than throwing.
Nested ordinary functions do not inherit
an inner `function` declared inside a method gets its own default binding. Use an arrow, or capture `const self = this`.