typescript / beginner
Snippet
Basic Class with Access Modifiers
Access modifiers like 'public' and 'private' control whether class members can be accessed from outside the class.
snippet.ts
1
2
3
4
5
6
7
8
class Account {private balance: number = 0;public owner: string;constructor(name: string) {this.owner = name;}}
Breakdown
1
private balance: number = 0;
The 'private' keyword prevents this property from being accessed or changed outside the class.
2
public owner: string;
The 'public' keyword (the default) allows access from anywhere.