Encapsulation 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.
1234567891011121314151617181920212223242526272829303132class 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)
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 6.25
Encapsulation in Classes
Pyyhkäise näyttääksesi valikon
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.
1234567891011121314151617181920212223242526272829303132class 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)
Kiitos palautteestasi!