Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Static Properties and Methods | Classes Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookStatic Properties and Methods

1234567891011
class MathHelper { static pi: number = 3.14159; static calculateCircleArea(radius: number): number { return MathHelper.pi * radius * radius; } } // Accessing static members without creating an instance console.log(MathHelper.pi); // 3.14159 console.log(MathHelper.calculateCircleArea(5)); // 78.53975
copy

When working with classes in TypeScript, you will encounter both static and instance members. Static members belong to the class itself, not to any specific object created from that class. You access static properties and methods using the class name, not through an instance. For example, MathHelper.pi and MathHelper.calculateCircleArea() are both accessed directly from the class.

In contrast, instance members are tied to individual objects created from the class. You must create an instance with new before you can access these members.

Use static members when you need functionality or data that is shared by all instances of a class, or when the data does not depend on instance-specific information. Common examples include:

  • Utility methods;
  • Constants;
  • Counters that are relevant to the class as a whole.
question mark

How do you access a static method in TypeScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

Suggested prompts:

Can you explain the difference between static and instance members with more examples?

When should I use static members instead of instance members?

Can you show how to define and use instance members in a class?

Awesome!

Completion rate improved to 5

bookStatic Properties and Methods

Swipe um das Menü anzuzeigen

1234567891011
class MathHelper { static pi: number = 3.14159; static calculateCircleArea(radius: number): number { return MathHelper.pi * radius * radius; } } // Accessing static members without creating an instance console.log(MathHelper.pi); // 3.14159 console.log(MathHelper.calculateCircleArea(5)); // 78.53975
copy

When working with classes in TypeScript, you will encounter both static and instance members. Static members belong to the class itself, not to any specific object created from that class. You access static properties and methods using the class name, not through an instance. For example, MathHelper.pi and MathHelper.calculateCircleArea() are both accessed directly from the class.

In contrast, instance members are tied to individual objects created from the class. You must create an instance with new before you can access these members.

Use static members when you need functionality or data that is shared by all instances of a class, or when the data does not depend on instance-specific information. Common examples include:

  • Utility methods;
  • Constants;
  • Counters that are relevant to the class as a whole.
question mark

How do you access a static method in TypeScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
some-alt