Challenge: Implement Private Properties in a Class
Task
You're creating a BankAccount class to manage users' bank accounts securely. Each account has an owner and a balance, but you want the balance to be protected from direct access. Your goal is to use private properties to prevent unintended modifications to the balance while allowing controlled access through methods.
- Private Balance Property:
- Inside the
BankAccountclass, declare a private property#balance.
- Inside the
- Constructor:
- The constructor should accept
ownerandinitialBalanceas parameters; - Assign
ownerto a public property andinitialBalanceto the private#balanceproperty.
- The constructor should accept
- Add Methods:
- deposit: Define a method that takes
amountas a parameter. If the amount is greater than 0, it should addamountto#balance; - withdraw: Define a method that takes
amountas a parameter. If the amount is greater than 0 and less than or equal to#balance, it should subtractamountfrom#balance; - getBalance: Define a method that returns a string with the owner's name and the account balance, e.g.,
"Account balance for John: $1500".
- deposit: Define a method that takes
123456789101112131415161718192021222324252627282930313233343536class BankAccount { #_____; // Declare private property constructor(owner, initialBalance) { this._____ = owner; this.#_____ = initialBalance; } deposit(_____) { if (_____) { this.#_____ += _____; } } withdraw(_____) { if (_____ && _____) { this.#_____ -= _____; } } getBalance() { return `Account balance for ${this._____}: $${this.#_____}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
- Declare a private property
#balancein theBankAccountclass; - In the constructor, assign
ownerto a public property andinitialBalanceto the private#balanceproperty; - Define a
depositmethod that takesamountas a parameter. Check ifamountis greater than 0, then addamountto#balance; - Define a
withdrawmethod that takesamountas a parameter. Check ifamountis greater than 0 and less than or equal to#balance, then subtractamountfrom#balance; - Define a
getBalancemethod that returns a string with the owner's name and the account balance.
123456789101112131415161718192021222324252627282930313233343536class BankAccount { #balance; // Declare private property constructor(owner, initialBalance) { this.owner = owner; this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } } getBalance() { return `Account balance for ${this.owner}: $${this.#balance}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Output: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Output: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain how private properties work in JavaScript classes?
What happens if I try to access the #balance property directly from outside the class?
Can you show how to add error handling for invalid deposit or withdrawal amounts?
Awesome!
Completion rate improved to 2.22
Challenge: Implement Private Properties in a Class
Sveip for å vise menyen
Task
You're creating a BankAccount class to manage users' bank accounts securely. Each account has an owner and a balance, but you want the balance to be protected from direct access. Your goal is to use private properties to prevent unintended modifications to the balance while allowing controlled access through methods.
- Private Balance Property:
- Inside the
BankAccountclass, declare a private property#balance.
- Inside the
- Constructor:
- The constructor should accept
ownerandinitialBalanceas parameters; - Assign
ownerto a public property andinitialBalanceto the private#balanceproperty.
- The constructor should accept
- Add Methods:
- deposit: Define a method that takes
amountas a parameter. If the amount is greater than 0, it should addamountto#balance; - withdraw: Define a method that takes
amountas a parameter. If the amount is greater than 0 and less than or equal to#balance, it should subtractamountfrom#balance; - getBalance: Define a method that returns a string with the owner's name and the account balance, e.g.,
"Account balance for John: $1500".
- deposit: Define a method that takes
123456789101112131415161718192021222324252627282930313233343536class BankAccount { #_____; // Declare private property constructor(owner, initialBalance) { this._____ = owner; this.#_____ = initialBalance; } deposit(_____) { if (_____) { this.#_____ += _____; } } withdraw(_____) { if (_____ && _____) { this.#_____ -= _____; } } getBalance() { return `Account balance for ${this._____}: $${this.#_____}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
- Declare a private property
#balancein theBankAccountclass; - In the constructor, assign
ownerto a public property andinitialBalanceto the private#balanceproperty; - Define a
depositmethod that takesamountas a parameter. Check ifamountis greater than 0, then addamountto#balance; - Define a
withdrawmethod that takesamountas a parameter. Check ifamountis greater than 0 and less than or equal to#balance, then subtractamountfrom#balance; - Define a
getBalancemethod that returns a string with the owner's name and the account balance.
123456789101112131415161718192021222324252627282930313233343536class BankAccount { #balance; // Declare private property constructor(owner, initialBalance) { this.owner = owner; this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } } getBalance() { return `Account balance for ${this.owner}: $${this.#balance}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Output: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Output: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
Takk for tilbakemeldingene dine!