What “this” means, decided by how you call a function
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.
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
| Call looks like | this is | Name |
|---|---|---|
| obj.f() | obj | method call |
| f() | undefined (strict) or globalThis (sloppy) | plain call |
| f.call(x) / f.apply(x) / f.bind(x)() | x | explicit |
| new f() | a new object | constructor 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.
- 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.
addAll(nums) {nums.forEach(function (n) {this.total += n; // this is undefined});}
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.
this.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.save = () => {...} 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().