Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Private Members | Access Modifiers in TypeScript
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookPrivate Members

When designing robust applications in TypeScript, it is important to control how the internal state of a class can be accessed and modified. The private access modifier allows you to define properties and methods that are hidden from outside the class. This means only the class itself can read or change these members, helping you hide implementation details and prevent accidental misuse. By marking a property or method as private, you ensure that it cannot be accessed, modified, or called from outside the class, including from subclasses.

1234567891011121314151617181920212223242526272829303132333435363738
class BankAccount { private balance: number; constructor(initialBalance: number) { this.balance = initialBalance; } private logTransaction(amount: number, type: string): void { console.log(`${type} of $${amount}. New balance: $${this.balance}`); } public deposit(amount: number): void { this.balance += amount; this.logTransaction(amount, "Deposit"); } public withdraw(amount: number): void { if (amount > this.balance) { console.log("Insufficient funds"); return; } this.balance -= amount; this.logTransaction(amount, "Withdrawal"); } public getBalance(): number { return this.balance; } } const account = new BankAccount(1000); account.deposit(200); // Deposit of $200. New balance: $1200 account.withdraw(500); // Withdrawal of $500. New balance: $700 console.log(account.getBalance()); // 700 // The following lines would cause a compilation error: // account.balance = 5000; // account.logTransaction(100, "Test");
copy

Encapsulation is a core principle of object-oriented programming. By using private members, you protect the integrity of your class's data and logic, making it easier to maintain and refactor code without breaking external components that depend on your class. Private members enforce clear boundaries between a class's internal workings and its public interface. This helps prevent accidental changes to internal state and allows you to safely update or optimize the class's implementation in the future, knowing that external code cannot rely on or interfere with its private details.

question mark

What happens if you try to access a private property from outside its class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain the difference between private and public members in TypeScript?

Why would I use private members instead of protected or public?

Can you show an example of what happens if I try to access a private member from outside the class?

Awesome!

Completion rate improved to 5

bookPrivate Members

Sveip for å vise menyen

When designing robust applications in TypeScript, it is important to control how the internal state of a class can be accessed and modified. The private access modifier allows you to define properties and methods that are hidden from outside the class. This means only the class itself can read or change these members, helping you hide implementation details and prevent accidental misuse. By marking a property or method as private, you ensure that it cannot be accessed, modified, or called from outside the class, including from subclasses.

1234567891011121314151617181920212223242526272829303132333435363738
class BankAccount { private balance: number; constructor(initialBalance: number) { this.balance = initialBalance; } private logTransaction(amount: number, type: string): void { console.log(`${type} of $${amount}. New balance: $${this.balance}`); } public deposit(amount: number): void { this.balance += amount; this.logTransaction(amount, "Deposit"); } public withdraw(amount: number): void { if (amount > this.balance) { console.log("Insufficient funds"); return; } this.balance -= amount; this.logTransaction(amount, "Withdrawal"); } public getBalance(): number { return this.balance; } } const account = new BankAccount(1000); account.deposit(200); // Deposit of $200. New balance: $1200 account.withdraw(500); // Withdrawal of $500. New balance: $700 console.log(account.getBalance()); // 700 // The following lines would cause a compilation error: // account.balance = 5000; // account.logTransaction(100, "Test");
copy

Encapsulation is a core principle of object-oriented programming. By using private members, you protect the integrity of your class's data and logic, making it easier to maintain and refactor code without breaking external components that depend on your class. Private members enforce clear boundaries between a class's internal workings and its public interface. This helps prevent accidental changes to internal state and allows you to safely update or optimize the class's implementation in the future, knowing that external code cannot rely on or interfere with its private details.

question mark

What happens if you try to access a private property from outside its class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt