Skip to main content

Classes

TypeScript takes an object-oriented approach and has full class support. The class represents a template for creating objects and encapsulates the functionality that an object must have. A class defines the state and behavior that an object has.

Classes#

Let's look at a simple example of working with classes:

class Greeter {
greeting: string // Свойство greeting, тип строка
constructor(message: string) {
// Конструктор класса Greeter
this.greeting = message
}
greet() {
// Метод класса Greeter
return 'Hello, ' + this.greeting
}
}
let greeter = new Greeter('world')
greeter.greet() // Вернет строчку Hello world

A new class Greeter has been declared. This class has three items: a greeting property, a constructor, and a greet method. The last line instantiates the Greeter class using new. It calls the constructor defined earlier, creates a new object, and runs the constructor to initialize it.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


Philipp Dvinyaninov

📖

Dmitriy Vasilev

💵

Become a Patron!