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

bookGetters and Setters

123456789101112131415161718192021222324
class BankAccount { private _balance: number; constructor(initialBalance: number) { this._balance = initialBalance; } get balance(): number { return this._balance; } set balance(amount: number) { if (amount < 0) { throw new Error("Balance cannot be negative"); } this._balance = amount; } } const account = new BankAccount(1000); console.log(account.balance); // 1000 account.balance = 1500; console.log(account.balance); // 1500 // account.balance = -500; // This will throw an error
copy

Getters and setters in TypeScript classes let you control how properties are accessed and modified. Instead of exposing a property directly, you can use a getter to retrieve its value and a setter to update it. This approach lets you add logic, such as validation or transformation, whenever a property is read or written. By using getters and setters, you can prevent invalid states, enforce rules, or trigger side effects, all while keeping the property itself private and protected from direct changes. This encapsulation is a key part of object-oriented programming, helping you maintain clean, predictable, and robust code as your applications grow.

question mark

What is the main benefit of using getters and setters in a class?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 5

bookGetters and Setters

Pyyhkäise näyttääksesi valikon

123456789101112131415161718192021222324
class BankAccount { private _balance: number; constructor(initialBalance: number) { this._balance = initialBalance; } get balance(): number { return this._balance; } set balance(amount: number) { if (amount < 0) { throw new Error("Balance cannot be negative"); } this._balance = amount; } } const account = new BankAccount(1000); console.log(account.balance); // 1000 account.balance = 1500; console.log(account.balance); // 1500 // account.balance = -500; // This will throw an error
copy

Getters and setters in TypeScript classes let you control how properties are accessed and modified. Instead of exposing a property directly, you can use a getter to retrieve its value and a setter to update it. This approach lets you add logic, such as validation or transformation, whenever a property is read or written. By using getters and setters, you can prevent invalid states, enforce rules, or trigger side effects, all while keeping the property itself private and protected from direct changes. This encapsulation is a key part of object-oriented programming, helping you maintain clean, predictable, and robust code as your applications grow.

question mark

What is the main benefit of using getters and setters in a class?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4
some-alt