Menu
Coddy logo textTech

Private 필드로 잔액 관리

Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 23번째.

challenge icon

챌린지

balance를 프라이빗 필드로 변환하여 BankAccount 클래스를 더 안전하게 만들어 봅시다.

수행할 작업:

  1. 클래스 상단에 #balance를 추가하세요.
  2. 생성자(constructor)에서 this.balancethis.#balance로 변경하세요.
  3. 모든 메서드에서 this.balancethis.#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


객체 지향 프로그래밍의 모든 레슨