Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

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

Awesome!

Completion rate improved to 6.25

bookEncapsulation in Classes

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
some-alt