Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 4.3Classes and access modifiers

Classes are useful when data and operations belong together. They are not a replacement for interfaces or plain objects; choose the smallest model that fits.

A class

TypeScript
class Counter {
  private value = 0;

  increment(): number {
    return ++this.value;
  }
}

Access modifiers

public is the default. private restricts access to the class, and readonly allows initialisation but prevents later reassignment. Use these to express an API, not simply to hide every field.

Feedback 👍
Comments are sent via email to me.