Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 12 / 31FUNCTIONS

The meaning of this.

this is determined by the call shape, except in arrow functions.

01 / THE BASICS

Start here.

An object can contain a function as one of its properties. We call that function a method when it is used as behavior of the object. In person.greet(), the expression before the dot is the receiver. For an ordinary method, this inside greet normally refers to that receiver. If person has name: 'Ada', then this.name reads 'Ada'. The word this is a special value supplied by the way the function is called.

A method is still a function value. Assigning const greet = person.greet copies that function value into a new binding. Calling greet() has no receiver before a dot, so this is no longer person. In modules and strict mode, this is undefined in that plain call, and reading this.name fails. Nothing about the function's original location permanently attached it to person.

An arrow function handles this differently: it takes this from the surrounding scope where the arrow was created. That makes arrows useful inside a method when an inner callback should keep the outer receiver. bind can also create a new function with a chosen receiver. Before using either, inspect the call shape: where is the function called, and which object, if any, appears before the dot?

02 / THE IDEA

Build the model.

In an ordinary function, this is associated with how the function is called. Calling obj.method() normally supplies obj as this. Extracting the method and calling it alone changes that relationship. Arrow functions instead inherit this from the surrounding lexical scope.

This is why passing an unbound method as a callback can break. When behavior belongs to an object, keep the receiver in the call or bind it intentionally. Arrow functions are useful for callbacks that should keep the surrounding this, but they are not a universal replacement for methods.

03 / UNDER THE SURFACE

How it works.

For an ordinary method call, the receiver before the dot usually becomes this inside the method. person.greet() therefore reads person.name. If you take the function out with const greet = person.greet and call greet(), there is no person receiver in that call. In modern module or strict code, this is undefined and trying to read this.name throws.

An arrow function has no call-time this of its own; it uses the surrounding this where it was created. That makes an arrow helpful inside a method callback that must keep the method's receiver. Use bind when you intentionally want an ordinary function to keep a particular receiver after passing it elsewhere. The point is to make the receiver relationship visible, not to memorize a single rule for all functions.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const person = { name: 'Ada', greet() { return this.name; } };
person.greet(); // 'Ada'

First notice. The receiver in person.greet() is visible before the dot.

The object literal creates a greet method. person.greet() invokes it with person as receiver. Inside greet, this.name reads 'Ada', and the function returns that string. The method is not permanently attached to person as a bound function; the call syntax supplies the receiver each time.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const counter = { value: 0, next() { return ++this.value; } };
const safeNext = counter.next.bind(counter);
console.log(safeNext(), counter.value);

The output is 1 1. bind creates a new function whose this is counter. Calling safeNext updates counter.value even though that call does not use counter.next(). Without bind, the extracted method would not have that receiver.

06 / KEEP THIS

One thing to remember.

For ordinary functions, inspect the call site before deciding what this means.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.