Grokking TypeScript
Vote

0% completed

​

Getters and Setters

Can reading a property run code? In a class, it can. The expression account.available below looks like a plain property read, but no property with that name is stored anywhere on the object.

class BankAccount { private balance: number; private overdraftLimit = 200; constructor(balance: number) { this.balance = balance; } get available(): number { return this.balance + this.overdraftLimit; } deposit(amount: number): void { this.balance += amount; } } const account = new BankAccount(100); console.log(account.available); // 300 account

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content