Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Public Members | Access Modifiers in TypeScript
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookPublic Members

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 5

bookPublic Members

Swipe um das Menü anzuzeigen

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt