Skip to content

What “this” means, decided by how you call a function

Working knowledgeConcept

The same function can see a different this every time it runs. The function’s definition does not decide; the line that calls it does, unless it is an arrow.

01 The same method, two answers

Copy a method into a variable and call it, and it forgets which object it belongs to.

user.js
class User {
constructor(name) { this.name = name; }
greet() { return `Hi, ${this.name}`; }
}
const ana = new User('Ana');
ana.greet(); // 'Hi, Ana'
const greet = ana.greet;
greet(); // TypeError: Cannot read properties of undefined (reading 'name')

Nothing about greet changed between the two calls. What changed is how it was called. That is the whole rule: for a normal function, this is not fixed when the function is written. It is filled in fresh on every call, from the call site.

02 Four ways to call, four answers

How the call site sets this for a normal function
Call looks likethis isName
obj.f()objmethod call
f()undefined (strict) or globalThis (sloppy)plain call
f.call(x) / f.apply(x) / f.bind(x)()xexplicit
new f()a new objectconstructor call

When more than one could apply, the stronger rule wins: new beats bind, bind beats call and apply, and an explicit this beats a method call. Arrow functions sit outside this table entirely; they come next.

this.mjs
function greet() {
return this === undefined ? 'undefined' : this.name;
}
const user = { name: 'user', greet };
const admin = { name: 'admin' };
console.log(user.greet()); // user
const loose = user.greet;
console.log(loose()); // undefined (module code is strict)
console.log(greet.call(admin)); // admin
function Greeter() { this.name = 'fresh'; }
console.log(new Greeter().name); // fresh
const bound = greet.bind(admin);
console.log(bound()); // admin
console.log(bound.call(user)); // admin: bind wins over call
const bound2 = bound.bind(user);
The same function, six call sites
Step 1 of 7
Call sites, in order
user.greet()
loose()
greet.call(admin)
new Greeter()
bound()
bound.call(user)
What this was
Empty
  • being called
  • this for that call
  • not called yet

One function, greet, and six ways to call it. The function body never changes; only the call does. Nothing has run yet.

03 Arrow functions have no this of their own

An arrow function does not get a this from its call. It uses the this of the code around it, exactly like any other outer variable. call, apply and bind cannot change it, and arrows cannot be called with new.

function callback
addAll(nums) {
nums.forEach(function (n) {
this.total += n; // this is undefined
});
}
Arrow callback
addAll(nums) {
nums.forEach((n) => {
this.total += n; // this is the Tally
});
}

forEach calls your callback as a plain call, so a normal function gets undefined inside a class. The arrow has no this of its own, so it sees addAll’s this, which is the object addAll was called on.

The flip side: an arrow used as a method does not get the object. { name: 'x', hi: () => this.name } reads this from the surrounding code (in a module, undefined), not from the object literal.

04 Where this gets lost

Every one of these passes a function somewhere without calling it. By the time it is called, the object on the left of the dot is no longer part of the call.

setTimeout(user.greet, 100);Passing a method as a callbackThe timer calls the bare function. Wrap it, setTimeout(() => user.greet(), 100), or pass user.greet.bind(user).
names.map(formatter.format);Array methodsSame problem: map calls format plainly. Use an arrow, or map’s second argument, names.map(formatter.format, formatter).
button.addEventListener('click', this.save);DOM event handlersA normal function used as a listener gets the element as this, not your object. Bind it once in the constructor, or define save = () => { ... } as a class field.
const { greet } = ana; greet();Destructuring a methodDestructuring is just const greet = ana.greet in disguise. The call has no dot, so this is lost.

05 bind fixes this once and for all

f.bind(x) returns a new function. It does not change f. Binding the result again does nothing: the first bind wins. The one exception is new: calling a bound function with new ignores the bound this and uses the fresh object, though bound arguments still apply.

JavaScript
function Point(x) { this.x = x; }
const Bound = Point.bind({ x: 'ignored' }, 5);
const p = new Bound();
p.x; // 5
p instanceof Point; // true
bind in a constructorOne copy per objectthis.save = this.save.bind(this) makes a bound function for each instance. The original method stays on the prototype, so super.save() still works in a subclass.
Arrow class fieldOne copy per objectsave = () => {...} does the same with less code. The function lives only on the instance, not the prototype, so a subclass cannot reach it with super.save().

06 Check yourself

Question

In an ES module, const f = obj.method; f(); What is this inside method?

Question

const g = f.bind(a); g.call(b); What is this inside f?

Question

Inside a class method, items.forEach(function () { this.count++; }) throws. Which change fixes it with the least code?

07 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?