스마트 홈 기기 시스템
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 56번째.
챌린지
이 챌린지에서는 스마트 홈에 얼마나 많은 기기가 있는지 모니터링할 수 있도록 SmartDevice 시스템에 정적 추적(static tracking) 기능을 추가해야 합니다.
1. SmartDevice 클래스에 정적 속성을 추가하세요:
- 클래스 상단에
static totalDevices = 0;를 추가합니다.
2. 생성자(constructor)에서 카운터를 증가시키세요:
SmartDevice생성자 안에SmartDevice.totalDevices++;를 추가합니다.
3. 개수를 가져오는 정적 메서드를 추가하세요:
static getDeviceCount() { return SmartDevice.totalDevices; }를 추가합니다.
직접 해보기
import { SmartDevice } from './device.js';
console.log(`Device count: ${SmartDevice.getDeviceCount()}`); // 0이어야 함
const basicDevice1 = new SmartDevice('Smart Plug');
const basicDevice2 = new SmartDevice(' Thermostat');
console.log(`Total devices now: ${SmartDevice.getDeviceCount()}`); // 2여야 함