Access Modifiers
Access modifiers control who can read or write class members.
Like locked drawers vs open shelves.
TypeScript checks them at compile time.
Compile time means before the program runs.
public, private, and protected
public means anyone can touch it. This is the default.
private means only the same class can touch it.
protected means the class and its children can touch it.
Children means subclasses that extend the parent.
readonly fields
readonly can mix with any access level.
The field is set in the constructor or at declaration.
Then you cannot reassign it.
Reassign means put a new value in that slot.
ECMAScript #private
True runtime privacy uses #field syntax.
Runtime means while the program is running.
Unlike TypeScript private, #private is enforced by JavaScript engines.
Not only the type checker.
Getters and setters
A getter is a controlled way to read a value.
A setter is a controlled way to write a value.
Check writes. Keep the public surface small.
Public surface means what outside code is allowed to touch.
Why hide things?
Hide what callers should not touch.
Then you can change the insides later without breaking them.
That is a big win in shared projects.
Hide what callers should not touch. Keep the public API small.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.