Modifiers

Public by default

Members are public unless marked otherwise. Public fields and methods form the normal way to use an object.

class User { public name: string = "Guest"; } const user = new User(); console.log(user.name);

Guest

private walls

Mark internals as private to hide them from outside code. Only methods of the same class can read or change a private member.

Outside access is a compile error, which keeps refactoring safe.

class Vault { private code: string = "1234"; unlock(attempt: string): boolean { return attempt === this.code; } } const vault = new Vault(); console.log(vault.unlock("1234"));

true

Protected for children

Protected members are hidden from outside code but visible inside child classes. Use protected for helpers that children need and strangers do not.

class Animal { protected legs: number = 4; } class Dog extends Animal { describe(): string { return "Legs: " + this.legs; } } console.log(new Dog().describe());

Legs: 4

Readonly fields

Mark a field as readonly when it is set once and never changed after. The compiler rejects later assignments to that field.

class Config { readonly host: string = "localhost"; } const config = new Config(); console.log(config.host);

localhost

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the access of a member with no modifier?

    Answer

    Public: any code that holds the object can use it

    Members are public unless a modifier says otherwise, and public mostly documents that choice.

  2. Who may read or change a private member?

    Answer

    Only methods of the same class

    The private wall stops outside access at compile time, which keeps internals free to change.

  3. Who may use a protected member?

    Answer

    The class and the classes that extend it

    Protected keeps the member away from strangers while child classes still see it.

  4. What does readonly on a field do?

    Answer

    It allows one assignment, then rejects later ones

    The field is set where it is declared or in the constructor, and a later assignment is a compile error.

  5. What does this program print? class User { public name: string = "Guest"; } const user = new User(); console.log(user.name);

    Answer

    Guest

    A public field is readable from outside, and it holds its initial value.

  6. What does this program print? class Vault { private code: string = "1234"; unlock(attempt: string): boolean { return attempt === this.code; } } const vault = new Vault(); console.log(vault.unlock("1234"));

    Answer

    true

    The method itself may read the private field, and the attempt matches it.

  7. What does this program print? class Animal { protected legs: number = 4; } class Dog extends Animal { describe(): string { return "Legs: " + this.legs; } } console.log(new Dog().describe());

    Answer

    Legs: 4

    The child class sees the protected field, so the method can build the text.

  8. What does this program print? class Config { readonly host: string = "localhost"; } const config = new Config(); console.log(config.host);

    Answer

    localhost

    A readonly field may be read freely; only a later assignment is rejected.

  9. What does the compiler say about this program? class Vault { private code: string = "1234"; } const vault = new Vault(); console.log(vault.code);

    Answer

    error TS2341: Property 'code' is private and only accessible within class 'Vault'.

    The private wall covers reads as well as writes, so the field is not reachable from outside.

  10. What does the compiler say about this program? class Config { readonly host: string = "localhost"; } const config = new Config(); config.host = "example.com"; console.log(config.host);

    Answer

    error TS2540: Cannot assign to 'host' because it is a read-only property.

    The field is readonly, so the assignment after creation stops the build.

Article author: Arthur Isaev