FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Objects, Prototypes & Classes

Advanced

Getters & Setters

Getters and setters make object properties behave like calculated or controlled values.

const profile = {
  first: "User",
  last: "A",
  get label() {
    return `${this.first} ${this.last}`;
  }
};

Objects, Prototypes & Classes

Getters read like properties while running code behind the scenes.

A getter is a method that is accessed like a property. It is useful for calculated values such as full names, labels, totals or derived state.

A setter is called when a property is assigned. It can validate, normalize or split input before storing it.

Getters and setters should stay predictable. They look like property access, so they should not do surprising work such as network calls or heavy side effects.

get

Creates a computed property read.

set

Runs when a property is assigned.

Derived state

A value calculated from other values.

Validation

A setter can reject or normalize invalid values.

Examples

Object code becomes clear when shape, state and behavior are visible.

Small derived value

const item = {
  title: "Overview",
  status: "ready",
  get label() {
    return `${this.title}: ${this.status}`;
  }
};

Getter with surprising work

const item = {
  get data() {
    return fetch("/api/data");
  }
};

// Property access should not hide async network work.

Code patterns

Reusable examples for quick reference.

These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.

Getter

Expose a calculated label as a property.

const profile = {
  name: "User A",
  role: "editor",
  get label() {
    return `${this.name} (${this.role})`;
  }
};

Setter

Normalize assigned values.

const state = {
  _status: "draft",
  set status(value) {
    this._status = value.trim().toLowerCase();
  }
};

Getter and setter pair

Control how a value is stored and read.

const record = {
  _title: "",
  get title() { return this._title; },
  set title(value) { this._title = value.trim(); }
};

Class getter

Classes can use the same get syntax.

class Counter {
  count = 0;
  get label() { return `Count: ${this.count}`; }
}

Rules that matter

Model data deliberately before adding behavior.

Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.

Use getters for derived values

They should calculate from existing state.

Use setters for normalization

Trim, validate or convert assigned values.

Avoid heavy side effects

Property access should feel cheap and predictable.

Name backing fields clearly

_title is a common convention, not privacy by itself.

Do not overuse magic properties

Plain methods are clearer when behavior is complex.

Remember access syntax

A getter is read without parentheses.

Production thinking

Object design decides how understandable the rest of the code can be.

Why does this matter?

Getters and setters are useful when a value should look like a property but still needs calculation or control.

Accessibility

Derived labels can keep accessible text consistent with state without duplicating strings everywhere.

Production note

Production getters should be deterministic and cheap. Complex behavior should usually be a named method.

SEO note

Computed labels and metadata should be predictable and avoid hidden async behavior.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Add a setter that changes status.
  • Replace the getter with a normal label() method.
  • Try an empty title and inspect the fallback.

Practice assignment

Do this before moving to the next topic.

  1. Create one getter for a derived label.
  2. Create one setter that trims input.
  3. Explain when a normal method would be clearer.

Try it yourself

Use a getter for a status label

Live preview

Self-check

Before you continue, prove that you understand Getters & Setters.

Advanced

Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.

  1. Can you explain what a getter is?
  2. Can you explain what a setter is?
  3. Can you explain derived state?
  4. Can you explain why getters should avoid heavy side effects?
  5. Can you explain why backing fields are only a convention?