Typescript. Access Modifiers

public // by default

private // only inside class

protected // only inside class and its inheritors

readonly // readonly πŸ™‚

a little bit of ts sugar….

this code

class Person {
     
    constructor(private name: string, private age: number) {  }
     
    printPerson(): void {
 
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
}

will be similar to that one

class Person {
     
    private name: string;
    private age: number;
 
    constructor(name: string, age: number) {
 
        this.name = name;
        this.age = age;
    }
    printPerson(): void {
 
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
}

and the same will be for public modifier

This entry was posted in Π‘Π΅Π· Ρ€ΡƒΠ±Ρ€ΠΈΠΊΠΈ. Bookmark the permalink.