Functions in Classes

In this lesson, we will cover the concept of functions in classes.

Functions and class in a nutshell

Functions written inside a class are a little bit different. In the next chapter, we will talk about the object and all the rich possibilities that TypeScript brings. In short, functions are similar but don’t use the keyword function. Instead, the explicit visibility of the function is used or sometimes, not at all.

The following code defines three functions. Line 2 is a private function. A private function can only be invoked inside the class. Line 3 is a public function. It can be called inside and outside the function. Finally, line 4 is a protected function. It can be called from the class but also from any class that extends the class.

Get hands-on with 1200+ tech skills courses.