Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Private Members | Access Modifiers in TypeScript
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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 5

bookPrivate Members

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt