Private 필드로 잔액 관리
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 23번째.
챌린지
balance를 프라이빗 필드로 변환하여 BankAccount 클래스를 더 안전하게 만들어 봅시다.
수행할 작업:
- 클래스 상단에
#balance를 추가하세요. - 생성자(constructor)에서
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