요약 챌린지
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 56개 중 6번째.
챌린지
복습 챌린지: 스마트 온도 조절기 만들기
스마트 온도 조절기 객체를 만들어 이 장에서 배운 내용을 모두 연습해 보세요.
할 일:
room매개변수를 받는Thermostat라는 생성자 함수를 만드세요.- 생성자 안에서
this를 사용해 다음 속성을 설정하세요:room(매개변수에서 가져옴)temperature(22로 설정)isOn(true로 설정)
this를 사용해 생성자 안에 다음 메서드를 추가하세요:increaseTemp()- 온도를 1도 높입니다.decreaseTemp()- 온도를 1도 낮춥니다.getStatus()- 정확히[room] thermostat: [temperature]°C를 반환합니다 (영어 단어 thermostat을 그대로 유지하세요).
직접 해보기
// TODO: room 매개변수를 받는 Thermostat 생성자 함수를 만드세요
// TODO: 생성자 안에서 다음 속성을 설정하세요:
// - room (매개변수에서 가져옴)
// - temperature (22로 설정)
// - isOn (true로 설정)
// TODO: temperature를 1도 높이는 increaseTemp() 메서드를 추가하세요
// TODO: temperature를 1도 낮추는 decreaseTemp() 메서드를 추가하세요
// TODO: "[room] thermostat: [temperature]°C"를 반환하는 getStatus() 메서드를 추가하세요
// 코드를 테스트하세요 - 이 부분은 수정하지 마세요
const livingRoom = new Thermostat("Living Room");
livingRoom.increaseTemp();
livingRoom.increaseTemp();
console.log(livingRoom.getStatus()); // 다음과 같이 출력되어야 합니다: "Living Room thermostat: 24°C"
const bedroom = new Thermostat("Bedroom");
bedroom.decreaseTemp();
bedroom.decreaseTemp();
console.log(bedroom.getStatus()); // 다음과 같이 출력되어야 합니다: "Bedroom thermostat: 20°C"
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 JavaScript 컴파일러