get
Creates a computed property read.
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
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.
Creates a computed property read.
Runs when a property is assigned.
A value calculated from other values.
A setter can reject or normalize invalid values.
Examples
const item = {
title: "Overview",
status: "ready",
get label() {
return `${this.title}: ${this.status}`;
}
};
const item = {
get data() {
return fetch("/api/data");
}
};
// Property access should not hide async network work.
Code patterns
These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.
Expose a calculated label as a property.
const profile = {
name: "User A",
role: "editor",
get label() {
return `${this.name} (${this.role})`;
}
};
Normalize assigned values.
const state = {
_status: "draft",
set status(value) {
this._status = value.trim().toLowerCase();
}
};
Control how a value is stored and read.
const record = {
_title: "",
get title() { return this._title; },
set title(value) { this._title = value.trim(); }
};
Classes can use the same get syntax.
class Counter {
count = 0;
get label() { return `Count: ${this.count}`; }
}
Rules that matter
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.
They should calculate from existing state.
Trim, validate or convert assigned values.
Property access should feel cheap and predictable.
_title is a common convention, not privacy by itself.
Plain methods are clearer when behavior is complex.
A getter is read without parentheses.
Production thinking
Getters and setters are useful when a value should look like a property but still needs calculation or control.
Derived labels can keep accessible text consistent with state without duplicating strings everywhere.
Production getters should be deterministic and cheap. Complex behavior should usually be a named method.
Computed labels and metadata should be predictable and avoid hidden async behavior.
Live code lab
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
Practice assignment
Try it yourself
Self-check
Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.