ProgramGuru

Class Methods

Understand Class Methods

A method is a function associated with a class or object. Inside a normal instance method, this usually identifies the receiver.

What you will learn: the purpose of class methods, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how class methods works and where its type rules apply.
  • Predict the exact output of the class methods example.
  • Diagnose a common issue: Passing counter.

Build the mental model

A method can read and update instance fields. Be careful when passing an unbound method as a standalone callback: JavaScript may lose the expected this receiver unless you bind it or wrap the call in an arrow function.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Class Methods: explained visuallyvalue = 0 → increment(2) → value = 2value = 0increment(2)value = 2Follow the values through the operation
Class Methods: value = 0 → increment(2) → value = 2.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

class Counter {
  value = 0;
  increment(step: number): number {
    this.value += step;
    return this.value;
  }
}
const counter = new Counter();
console.log(counter.increment(2));
console.log(counter.increment(3));
console.log(counter.value);

Verified output

2
5
5

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The first call adds 2 to value 0 and returns 2. The next call adds 3 to the existing value and returns 5. Reading counter.value confirms the stored state is 5.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Passing counter.increment directly to another function can lose this; use a wrapper like () => counter.increment(1) when appropriate.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Add decrement(step) and test that it changes the same instance state.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What does this.value refer to inside counter.increment(2)?
Choose one answer. You can try again.

Answer and explanation

The value property of the counter instance receiving the call.

A method is a function associated with a class or object. Inside a normal instance method, this usually identifies the receiver.

Review the key idea

Class Methods: A method is a function associated with a class or object.

The first call adds 2 to value 0 and returns 2. The next call adds 3 to the existing value and returns 5. Reading counter.value confirms the stored state is 5.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Access Modifiers →