Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Private Fields in Classes | Inheritance and Encapsulation
JavaScript Classes and OOP Foundations

bookPrivate Fields in Classes

Private fields in JavaScript classes give you a way to hide data from outside code, reinforcing the principle of encapsulation. When you use the # symbol before a field name in a class, you create a private field. This means the field is only accessible within the class body itself—code outside the class cannot read or write to it directly. By restricting access to sensitive data, you prevent accidental modification and help keep your objects' internal state safe. This is especially important in situations where exposing data could lead to bugs or security issues.

123456789101112131415161718192021222324252627282930
class BankAccount { #balance; // private field constructor(initialBalance) { this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; return amount; } return 0; } getBalance() { return this.#balance; } } const account = new BankAccount(100); account.deposit(50); console.log(account.getBalance()); // 150 console.log(account.#balance); // SyntaxError: Private field '#balance' must be declared in an enclosing class
copy
question mark

Which statements about private fields in JavaScript classes and encapsulation are true?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain why using private fields is important in JavaScript classes?

How do I access or modify a private field from outside the class?

Are there alternatives to private fields for encapsulation in JavaScript?

Awesome!

Completion rate improved to 6.25

bookPrivate Fields in Classes

Swipe um das Menü anzuzeigen

Private fields in JavaScript classes give you a way to hide data from outside code, reinforcing the principle of encapsulation. When you use the # symbol before a field name in a class, you create a private field. This means the field is only accessible within the class body itself—code outside the class cannot read or write to it directly. By restricting access to sensitive data, you prevent accidental modification and help keep your objects' internal state safe. This is especially important in situations where exposing data could lead to bugs or security issues.

123456789101112131415161718192021222324252627282930
class BankAccount { #balance; // private field constructor(initialBalance) { this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; return amount; } return 0; } getBalance() { return this.#balance; } } const account = new BankAccount(100); account.deposit(50); console.log(account.getBalance()); // 150 console.log(account.#balance); // SyntaxError: Private field '#balance' must be declared in an enclosing class
copy
question mark

Which statements about private fields in JavaScript classes and encapsulation are true?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 7
some-alt