typescript / beginner
Snippet
Private Class Members
The private modifier restricts access to a property so it can only be used inside the class. This is a core principle of encapsulation in object-oriented programming.
snippet.ts
1
2
3
4
class User {private id: number = 101;public name: string = "Alice";}
Breakdown
1
private id: number = 101;
Defines a property that cannot be seen or modified from outside the User class.
2
public name: string = "Alice";
Defines a property that is visible and can be accessed by any code that has an instance of the class.