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

bookEncapsulation in Classes

Encapsulation is a core principle of object-oriented programming (OOP) that allows you to control how data and behavior are accessed and modified within your classes. In JavaScript, encapsulation means that a class can hide its internal details, such as properties or helper methods, and only expose the parts that are necessary for other code to interact with. This helps to prevent unwanted interference or misuse of internal data, making your code more robust and easier to maintain.

When you design a class, you decide which data and methods should be accessible from outside the class (the public interface), and which should remain hidden or restricted to internal use. By carefully managing this boundary, you can ensure that the internal state of your objects cannot be changed in unexpected ways, and you can update the inner workings of your classes without affecting other parts of your code that rely on them.

1234567891011121314151617181920212223242526272829303132
class BankAccount { constructor(owner, initialBalance) { this.owner = owner; // Public property let balance = initialBalance; // Internal variable (not directly accessible from outside) // Public method to get the current balance this.getBalance = function () { return balance; }; // Public method to deposit money this.deposit = function (amount) { if (amount > 0) { balance += amount; } }; // Public method to withdraw money this.withdraw = function (amount) { if (amount > 0 && amount <= balance) { balance -= amount; } }; } } const account = new BankAccount("Alex", 100); account.deposit(50); account.withdraw(30); console.log(account.owner); // Output: Alex console.log(account.getBalance()); // Output: 120 console.log(account.balance); // Output: undefined (balance is encapsulated)
copy
question mark

Which of the following is a key benefit of using encapsulation when designing classes?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how encapsulation is achieved in this JavaScript example?

What are the benefits of using encapsulation in classes like BankAccount?

How can I make properties private in modern JavaScript?

Awesome!

Completion rate improved to 6.25

bookEncapsulation in Classes

Scorri per mostrare il menu

Encapsulation is a core principle of object-oriented programming (OOP) that allows you to control how data and behavior are accessed and modified within your classes. In JavaScript, encapsulation means that a class can hide its internal details, such as properties or helper methods, and only expose the parts that are necessary for other code to interact with. This helps to prevent unwanted interference or misuse of internal data, making your code more robust and easier to maintain.

When you design a class, you decide which data and methods should be accessible from outside the class (the public interface), and which should remain hidden or restricted to internal use. By carefully managing this boundary, you can ensure that the internal state of your objects cannot be changed in unexpected ways, and you can update the inner workings of your classes without affecting other parts of your code that rely on them.

1234567891011121314151617181920212223242526272829303132
class BankAccount { constructor(owner, initialBalance) { this.owner = owner; // Public property let balance = initialBalance; // Internal variable (not directly accessible from outside) // Public method to get the current balance this.getBalance = function () { return balance; }; // Public method to deposit money this.deposit = function (amount) { if (amount > 0) { balance += amount; } }; // Public method to withdraw money this.withdraw = function (amount) { if (amount > 0 && amount <= balance) { balance -= amount; } }; } } const account = new BankAccount("Alex", 100); account.deposit(50); account.withdraw(30); console.log(account.owner); // Output: Alex console.log(account.getBalance()); // Output: 120 console.log(account.balance); // Output: undefined (balance is encapsulated)
copy
question mark

Which of the following is a key benefit of using encapsulation when designing classes?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt