Getters & Setters
Getters & Setters
Section titled “Getters & Setters”Introduction
Section titled “Introduction”Getters and setters are special methods that let you define computed properties on a class. They look like regular property access but run code underneath.
Basic Getter
Section titled “Basic Getter”class User { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; }
get fullName() { return `${this.firstName} ${this.lastName}`; }}
const user = new User('Alice', 'Johnson');console.log(user.fullName); // "Alice Johnson" — no () needed!Getter and Setter Together
Section titled “Getter and Setter Together”class Temperature { constructor(celsius) { this._celsius = celsius; // convention: _ for internal }
get celsius() { return this._celsius; }
set celsius(value) { if (value < -273.15) throw new Error('Below absolute zero!'); this._celsius = value; }
get fahrenheit() { return this._celsius * 9/5 + 32; }
set fahrenheit(value) { this._celsius = (value - 32) * 5/9; }}
const temp = new Temperature(0);console.log(temp.celsius); // 0console.log(temp.fahrenheit); // 32
temp.celsius = 100;console.log(temp.fahrenheit); // 212
temp.celsius = -300; // ❌ Error: Below absolute zero!Practical Examples
Section titled “Practical Examples”Computed Properties
Section titled “Computed Properties”class Rectangle { constructor(width, height) { this.width = width; this.height = height; }
get area() { return this.width * this.height; }
get perimeter() { return 2 * (this.width + this.height); }
set dimensions({ width, height }) { this.width = width; this.height = height; }}
const rect = new Rectangle(3, 4);console.log(rect.area); // 12console.log(rect.perimeter); // 14
rect.dimensions = { width: 5, height: 6 };console.log(rect.area); // 30Validation with Setters
Section titled “Validation with Setters”class BankAccount { constructor(owner) { this.owner = owner; this._balance = 0; }
get balance() { return this._balance; }
set balance(value) { if (value < 0) throw new Error('Balance cannot be negative'); this._balance = value; }}
const account = new BankAccount('Alice');account.balance = 1000; // ✅console.log(account.balance); // 1000
account.balance = -500; // ❌ Error!Getters/Setters vs Regular Methods
Section titled “Getters/Setters vs Regular Methods”class Example { get computed() { return 'Accessed as a property'; }
computedMethod() { return 'Accessed as a method with ()'; }}
const ex = new Example();ex.computed; // property-style accessex.computedMethod(); // method-style callThe _ Convention
Section titled “The _ Convention”Prefixing internal properties with _ is a naming convention (not enforced):
class Person { constructor(name) { this._name = name; // _ indicates "internal, don't access directly" }
get name() { return this._name; }
set name(value) { if (!value) throw new Error('Name is required'); this._name = value; }}Summary
Section titled “Summary”- Getters/setters define computed properties
- Called without parentheses (like properties)
- Setters enable validation on assignment
- Getters provide read-only computed values
- Use
_prefix convention for internal storage