プライベートフィールドとしての残高
CoddyのJavaScriptジャーニー「オブジェクト指向プログラミング」セクションの一部 — レッスン 23/56。
チャレンジ
balance をプライベートフィールドに変換することで、BankAccount クラスをより安全にしましょう。
あなたのタスク:
- クラスの先頭に
#balanceを追加してください - コンストラクタ内の
this.balanceをthis.#balanceに変更してください - すべてのメソッド内の
this.balanceをthis.#balanceに変更してください
自分で試してみよう
import { BankAccount } from './bankAccount.js';
// 実装をテストする
const testAccount = new BankAccount('Alex Johnson', 500);
console.log(testAccount.getAccountInfo()); // "Alex Johnson: $500"
console.log('Balance:', testAccount.getBalance()); // 引き続き動作する: 500
console.log('Balance:', testAccount.balance); // undefined