Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Class Properties and Initialization | Classes Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookClass Properties and Initialization

123456789101112131415
class Person { name: string = "John"; // Property with a default value age: number; // Property without a default value isEmployed: boolean; // Property without a default value constructor(age: number, isEmployed: boolean) { this.age = age; // Initialize property in constructor this.isEmployed = isEmployed; } } const person1 = new Person(30, true); console.log(person1.name); // Output: John console.log(person1.age); // Output: 30 console.log(person1.isEmployed); // Output: true
copy

When working with TypeScript classes, you can declare properties directly inside the class body. Properties can be given default values, or you can leave them uninitialized and assign values later, usually in the constructor. Declaring a property with a default value means every instance of the class starts with that value unless it is overwritten. If you do not provide a default value, you must assign one—typically in the constructor—so that each instance can have its own value.

Using the constructor to initialize properties is especially useful for properties that should be set differently for each instance. This ensures that every object created from the class has its own unique set of property values, rather than sharing a single value across all instances.

question mark

What is the main advantage of initializing class properties in the constructor?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain what happens if I don't initialize a property in the constructor or with a default value?

How can I add more properties to the Person class?

What is the difference between public, private, and protected properties in TypeScript classes?

Awesome!

Completion rate improved to 5

bookClass Properties and Initialization

Свайпніть щоб показати меню

123456789101112131415
class Person { name: string = "John"; // Property with a default value age: number; // Property without a default value isEmployed: boolean; // Property without a default value constructor(age: number, isEmployed: boolean) { this.age = age; // Initialize property in constructor this.isEmployed = isEmployed; } } const person1 = new Person(30, true); console.log(person1.name); // Output: John console.log(person1.age); // Output: 30 console.log(person1.isEmployed); // Output: true
copy

When working with TypeScript classes, you can declare properties directly inside the class body. Properties can be given default values, or you can leave them uninitialized and assign values later, usually in the constructor. Declaring a property with a default value means every instance of the class starts with that value unless it is overwritten. If you do not provide a default value, you must assign one—typically in the constructor—so that each instance can have its own value.

Using the constructor to initialize properties is especially useful for properties that should be set differently for each instance. This ensures that every object created from the class has its own unique set of property values, rather than sharing a single value across all instances.

question mark

What is the main advantage of initializing class properties in the constructor?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt