← Fundamentals
Fundamentals

Methods, receivers, and this

How does a method get the value it operates on?

Follow the receiver to see which value a method reads or changes.

Outcome

Name a method’s receiver, predict whether it reaches the original or a copy, keep the right receiver when a method is passed as a value or called through another type, and call changing methods on the elements themselves.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Method
A function attached to a type and called on a value of it.
Receiver
The value a method is called on: this, self, or Go’s receiver parameter.
Value receiver
A receiver initialized from a value copy. Direct scalar-field writes affect that copy; copied pointer or reference fields can still lead to shared data.
Bound method
A method value that remembers which receiver to use.
Bound receiver
The object passed as a stored callback’s receiver. A stored receiver copy is a distinct object; its fields may still refer to other shared data.

The value behind a method call

A method is an operation associated with a type or object. Its receiver is the value the operation runs on: in log.appendSession(minutes), log supplies that receiver and minutes is an ordinary argument.

Reading a method, saving it for later and invoking it are separate steps. Trace what the saved callable retains, then which value the eventual call reaches; receiver rules differ between languages.

An ordinary TypeScript method uses this for its receiver. The call expression supplies this; a function value read from an object does not automatically retain that object. A static method operates on the class side rather than an instance.

01The receiver

A method receives its object as a hidden or explicit first argument

A team imports work-session durations into personal logs. Each selected person attended the same sessions, so the importer must update every person’s existing log. A buffered replay goes through a recorder. Which value does each method receive?

The importer first needs one person’s existing log to retain new durations. Trace the receiver of the calls below and predict what the caller reads afterward.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
class WorkLog {
  total = 0;

  appendSession(minutes: number): void {
    this.total += minutes;
  }
}

const log = new WorkLog();
log.appendSession(20);
log.appendSession(15);

console.log(log.total);
What is printed?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc --noEmit --strict main.ts && node main.ts

Use the setup from the first reading for your language.

02A method as a value

A method passed as a value keeps its receiver, a copy of it, or nothing

A buffered import stores an action for later replay. Before wiring it to a control, check what a saved method keeps: the live log, an earlier receiver value, or no receiver at all.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
class WorkLog {
  total = 0;

  appendSession(minutes: number): void {
    this.total += minutes;
  }
}

const log = new WorkLog();
const press = log.appendSession;

try {
  press(20);
} catch (error) {
  console.log((error as Error).name);
}

console.log(log.total);
What is printed?

03No receiver

Functions that build or work on a type without a receiver

A new log can also be initialized from a batch before any instance is selected. These creation functions build the receiver state for later calls; predict the returned total.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
class WorkLog {
  total = 0;

  static fromSessions(entries: number[]): WorkLog {
    const log = new WorkLog();
    log.total = entries.reduce((sum, entry) => sum + entry, 0);

    return log;
  }
}

console.log(WorkLog.fromSessions([20, 15]).total);
What is printed?

04Repair the work-session import

One log, a team import, then buffered replay

First append durations to one person’s log. Then import each attended session into every selected person’s log and reconcile minutes and entry counts. Finally replay the buffered durations through another object into the same log type.

Each file contains its imports, reader and complete caller.

Repair the work-session import

Retain one log’s updates, reconcile all selected people’s logs, then replay through the intended live receiver.

  1. easy · F-METHOD-V4-1 Record each entry
  2. medium · F-METHOD-V4-2 Import every person’s log
  3. hard · F-METHOD-V4-3 Replay entries through a recorder
In the frontend Register a callback that invokes the intended recorder method. Optional

A browser replay control receives a recorder from its client-side owner. Its ordinary flush method reads that recorder’s instance fields. Registering a callback is separate from choosing the object on which flush is called.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
"use client";

type Recorder = { label: string; flush(): void };

export default function ReplayControl({ recorder }: { recorder: Recorder }) {
  return <button onClick={() => recorder.flush()}>Replay {recorder.label}</button>;
}

The owner is another client-side component, so this function-valued recorder prop does not cross a server serialization boundary. On a click the wrapper performs recorder.flush(), supplying recorder as the method receiver. The recorder owns the replay work and any state publication.

Sources: React: responding to events,Svelte: event attributes,TypeScript: this in functions.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.