The this Keyword in JavaScript Explained
On this page
Learning Objective
Determine the value of this in any call using the four binding rules, explain why arrow functions behave differently, fix lost method context in callbacks with bind, arrow functions and class fields, and recognize this in event handlers and classes.
Concept Explanation
Most variables in JavaScript are lexically scoped, but this is different: for ordinary functions it is decided at call time, by how the function is called. The same function can see a different this on every call. That flexibility is useful for methods shared between objects, but it also causes one of the most common bugs in JavaScript: a method that works when called as obj.method() and breaks when passed somewhere as a callback.
There are four rules, checked in order of precedence. First, new binding: when a function is called with new, this is the freshly created object. Second, explicit binding: call, apply and bind set this to the object you pass. bind returns a new function permanently tied to that object. Third, implicit binding: when a function is called as a property of an object, obj.method(), this is the object before the dot. Fourth, default binding: a plain call such as fn() gets undefined in strict mode, which includes all modules and classes, or the global object in old sloppy mode scripts.
Implicit binding is easy to lose. const f = obj.method copies the function without the object, so calling f() falls back to default binding. Passing obj.method to setTimeout, to an array method or to addEventListener does the same thing. In strict mode this becomes undefined and the method throws a TypeError when it tries to read a property. In sloppy mode it silently reads properties of window, which is worse.
Arrow functions do not have their own this. They capture this from the surrounding scope at the moment they are created, exactly like any other variable, and call, apply, bind and new cannot change it. That makes arrows ideal for callbacks inside methods, where you want the method's this, and wrong for object methods or prototype methods that should receive the calling object, because an arrow defined at the top of an object literal captures the outer this, not the object.
Classes combine these rules. Methods defined in the class body live on the prototype and use normal call-time binding, so they lose this when detached. Class fields written as arrow functions, handleClick = () => {...}, are created per instance and capture the instance, which makes them safe to pass as callbacks at the cost of one function per instance. In DOM event listeners registered with a regular function, this is the element the listener is attached to, the same as event.currentTarget; with an arrow function, it is whatever this was outside.
| Call form | Rule | this is |
|---|---|---|
| new Fn() | new binding | The newly created object |
| fn.call(obj), fn.apply(obj), bound() | Explicit binding | obj |
| obj.method() | Implicit binding | obj |
| fn() in strict mode | Default binding | undefined |
| Arrow function | Lexical | this of the enclosing scope |
| Listener as a regular function | Set by the DOM | The element, same as currentTarget |
| Detached method const f = obj.m | Default binding | undefined, so f() throws |
Worked Example: A Class That Loses Its Context and Three Fixes
The SearchBox class works when methods are called directly but fails when a method is passed as an event listener.
class SearchBox {
constructor(input) {
this.input = input;
this.query = '';
input.addEventListener('input', this.onInput); // passes the function without the object
}
onInput(event) {
this.query = event.target.value; // this is the input element, not the SearchBox
this.render(); // TypeError: this.render is not a function
}
render() { console.log('Searching for', this.query); }
}
// Fix 1: bind once in the constructor
class SearchBoxBound {
constructor(input) {
this.query = '';
this.onInput = this.onInput.bind(this);
input.addEventListener('input', this.onInput); // the same reference can be removed later
}
onInput(event) { this.query = event.target.value; this.render(); }
render() { console.log('Searching for', this.query); }
}
// Fix 2: an arrow class field captures the instance
class SearchBoxField {
query = '';
constructor(input) { input.addEventListener('input', this.onInput); }
onInput = (event) => { this.query = event.target.value; this.render(); };
render() { console.log('Searching for', this.query); }
}
// Fix 3: wrap the call in an arrow at the call site
class SearchBoxWrapped {
constructor(input) {
this.query = '';
input.addEventListener('input', (e) => this.onInput(e)); // implicit binding inside the arrow
}
onInput(event) { this.query = event.target.value; this.render(); }
render() { console.log('Searching for', this.query); }
}
// Arrow functions are wrong as object methods
const counter = {
count: 0,
incBad: () => { this.count++; }, // this is the module's this, undefined
inc() { this.count++; }, // implicit binding: counter
};Fix 1 and Fix 2 also make removeEventListener work, because they keep a stable function reference. Fix 3 is concise, but the anonymous arrow cannot be removed later unless you store it or use an AbortSignal.
Real-World Examples
- Before React hooks, React class components required developers to bind event handlers in the constructor or use arrow class fields, and the React documentation of that era explained the bind step specifically because methods passed as onClick lost their this.
- The TypeScript compiler option noImplicitThis, enabled by strict mode, reports an error when this has an implicit any type, catching many detached method bugs at compile time instead of in production.
Review Questions & Answers
setTimeout receives only the function value, not the object, and later calls it as a plain function. With no object before the dot, default binding applies, so this is undefined in strict mode, or the global object in sloppy mode.
They do not have their own this. They use the this of the scope in which they were defined, like any captured variable, and call, apply, bind and new cannot change it.
For a listener defined as a regular function, this is the element the listener was registered on, the same as event.currentTarget. For an arrow function listener, this is inherited from the surrounding scope.
They are bound to the instance and safe to pass as callbacks, but each instance gets its own function object instead of sharing one method on the prototype, which uses more memory, and they cannot be overridden through super in subclasses in the usual way.