Grokking TypeScript
Vote

0% completed

​

Access Modifiers

The BankAccount class from the previous lesson allows the following code, and the compiler reports nothing wrong with it:

class BankAccount { owner: string; balance: number; constructor(owner: string, balance: number = 0) { this.owner = owner; this.balance = balance; } } const account = new BankAccount("Alice", 100); account.balance = -5000; console.log(account.balance); // -5000

Any code in the program can set the balance to any number, so no single place in the code can enforce a rule such as "the balance cannot go below zero"

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content