Menu
Coddy logo textTech

Balance as Private Field

Part of the Object Oriented Programming section of Coddy's JavaScript journey. Lesson 23 of 56.

challenge icon

Challenge

Let's make the BankAccount class more secure by converting the balance to a private field."

Your task:

  1. Add #balance at the top of the class
  2. Change this.balance to this.#balance in the constructor
  3. Change this.balance to this.#balance in all methods

Try it yourself

import { BankAccount } from './bankAccount.js';

// Test the implementation
const testAccount = new BankAccount('Alex Johnson', 500);

console.log(testAccount.getAccountInfo()); // "Alex Johnson: $500"
console.log('Balance:', testAccount.getBalance()); // still works: 500
console.log('Balance:', testAccount.balance); // undefined


All lessons in Object Oriented Programming

Practice on your own: Online JavaScript compiler