`this` is not determined by where a function is written, it's determined by HOW it's called. Four binding rules, ranked by precedence, explain every `this` value you'll ever encounter.
thiscallapplybindarrow-functionsfundamentals
Curated Video Reference
YouTube
Hand-picked video lecture to deepen your understanding of `this` Binding, call/apply/bind
Four `this` rules, HIGHEST precedence first:
1. new Fn() โ this = the newly created object
2. fn.call(obj) /
fn.apply(obj) /
fn.bind(obj)() โ this = obj, explicitly
3. obj.method() โ this = obj (whatever is left of the dot)
4. fn() โ this = undefined (strict) / globalThis (sloppy)
Arrow functions: NO own `this`, resolved lexically via scope chain,
fixed permanently at the arrow's DEFINITION site. Calling style is irrelevant.
call(obj, a, b) โ invoke NOW, args individually
apply(obj, [a, b]) โ invoke NOW, args as an array
bind(obj) โ returns a NEW function, this locked in, NOT invoked yet
Rule of thumb:
Object methods โ regular function (needs implicit binding)
Callbacks/timers/etc โ arrow function (needs to inherit outer this)
The beginner framing: this is a special keyword inside a function that refers to "the object the function is being run as a method of", but which object that actually is depends entirely on how you call the function, not where you wrote it.
The precise mental model: unlike every other binding you've seen so far, this is not resolved via the scope chain. It's a special value set fresh on the execution context (see Execution Context) every time a regular function is called, and the value depends on the call site, the exact expression used to invoke the function. There are four rules, and you apply them in this precedence order (highest wins):
new binding, new Fn() โ this is the newly created object.
Explicit binding, fn.call(obj), fn.apply(obj), or a function created with fn.bind(obj) โ this is exactly obj, no matter how it's later invoked.
Implicit binding, obj.method() โ this is obj, the object immediately to the left of the dot at the call site.
Default binding, a bare fn() call with no object context โ this is undefined in strict mode (or the global object in non-strict/sloppy mode).
The engine-level view: arrow functions are the one big exception to all of this. An arrow function has no this of its own, it doesn't get a this binding set on its execution context at all. Instead, any this reference inside an arrow function is resolved via the normal scope chain, exactly like any other variable, walking outward to find the nearest enclosingthis, captured lexically, at the arrow function's definition site, permanently.
`this` is decided at the call site
step 1 / 4
const obj = {
name: "obj",
regular() { return this.name; },
arrow: () => this.name,
};
ย
const fn = obj.regular;
ย
obj.regular();
fn();
obj.regular.call({ name: "custom" });
obj.arrow();
obj.regular()
implicit binding
this: obj (the object left of the dot)
Called as obj.method() โ this = obj. This is implicit binding, the most common case.
function introduce(greeting) { return `${greeting}, I'm ${this.name}`;}const person = { name: "Sam" };introduce.call(person, "Hi"); // .call, args passed individuallyintroduce.apply(person, ["Hi"]); // .apply, args passed as an arrayconst bound = introduce.bind(person);bound("Hi"); // .bind, returns a NEW function, permanently bound
bind is the only one of the three that doesn't call the function immediately, it returns a new function with this (and optionally some leading arguments) permanently locked in, immune to being re-bound by implicit or default binding later.
function Person(name) { this.name = name; // `this` here is the brand-new object `new` just created}const alice = new Person("Alice");alice.name; // "Alice"
new overrides every other rule, even if Person were called as obj.Person(...), using new still creates a fresh object and binds this to it.
startRegular()'s callback logs NaN repeatedly (this is undefined/the global object inside the callback, setInterval calls it as a bare function, default binding, so this.seconds is either a crash in strict mode or undefined + 1 = NaN in sloppy mode).
startArrow()'s callback correctly increments and logs 1, 2, 3, ..., the arrow function has no this of its own, so it uses startArrow's this, which is timer (implicit binding from ).
The real Function.prototype.bind also has a subtlety this version skips: a bind-created function used as a constructor (new boundFn()) ignores the bound this and uses the new instance instead, new binding beats even explicit binding. Try extending myBind to handle that case using new.target or a prototype check.
Class components made this topic unavoidable, event handler methods lose their this when passed as a bare reference (onClick={this.handleClick} is a default-binding call site, identical to the fn() example above), which is why you'd see constructors full of this.handleClick = this.handleClick.bind(this), or handlers written as class fields with arrow functions (handleClick = () => {...}) specifically to lock in this lexically.
Function components sidestep almost all of this, hooks don't use this at all, and event handlers are usually defined as regular const functions or arrow functions inside the component, closing over the values they need directly rather than reading them off this. If you still see a this-related bug in modern React, it's almost always inside a class component, or a plain (non-React) callback like the setInterval example above.
const { regular } = obj; // detaches the method from `obj`regular(); // undefined, default binding, `obj.` context is gone
Destructuring a method is exactly equivalent to const fn = obj.method, it silently strips the implicit binding. Either keep the method attached (obj.regular()) or .bind(obj) it first.
class Logger { prefix = "[LOG]"; log(msg) { console.log(this.prefix, msg); }}const logger = new Logger();[1, 2, 3].forEach(logger.log); // TypeError-ish: this.prefix is undefined
forEach calls the passed function as a bare function, default binding, completely unaware it was ever logger.log. Fix: .forEach((msg) => logger.log(msg)), or .bind(logger), or make log an arrow class field.
3. Using an arrow function for an object method that needs dynamic this#
const counter = { count: 0, increment: () => { this.count++; }, // โ arrow, `this` is NOT `counter`};counter.increment(); // this.count silently refers to outer scope's `this`, not counter
Arrow functions are wrong for object methods precisely because they don't participate in implicit binding at all, this inside increment is whatever this was in the surrounding scope where the object literal was written (often the module scope, where this is undefined).
Use regular functions for object methods, so implicit binding works as expected.
Use arrow functions for callbacks that should inherit this from their surrounding context (timers, event listeners, array methods, promise callbacks), this is the primary reason arrow functions exist.
Prefer bind over manually saving const self = this (an old pre-arrow-function pattern) when you need to permanently lock a function's this.
Never mix new with a function designed as a plain callback, and vice versa, a function's calling convention should be unambiguous from how it's written and named.
.bind() allocates a new function object every time it's called, creating a bound function inside a render function or a hot loop (onClick={this.handleClick.bind(this)} in JSX, evaluated on every render) creates unnecessary garbage. Bind once (in a constructor, or as a class field) rather than on every call/render.
.call() and .apply() are effectively free, the only difference between them and a normal call is how arguments are passed, with negligible overhead either way.
timer.startArrow()
This exact pattern, losing this in a callback passed to a timer, event listener, or array method, is the single most common this bug in real code, and it's why arrow functions became the default choice for callbacks.