// 데이터 // <여기에 class를 작성하세요.> const x = new Wizard(545, 210, 10); console.log(x.health, x.mana, x.armor); x.attack(); // 출력 // 545 210 10 // 파이어볼
문제 내용 중,
const x = new Wizard(545, 210, 10);
라는 코드가 나오는데, 이것은 Wizard라는 class의 x라는 Wizard 객체를 생성한 것이다. 객체가 생성되면 constructor 매소드는 자동으로 호출된다.
문제 속에 4개의 속성이 보인다.
console.log(x.health, x.mana, x.armor);
x.attack();
해당 클래스에 health, mana, armor라는 세 가지 초기 속성을 가지도록 class를 생성한다. 또한 attack 이라는 다른 속성도 하나 필요해 보인다.
'class' 키워드를 통해 class를 생성할 수 있다.
constructor ()라는 매소드를 추가하여 속성을 구성한다.
class Wizard {
contructor(health, mana, armor) {
this.health = health;
this.mana = mana;
this.armore = armor;
}
}
'파이어볼'이라는 내용이 표기되는 attack 속성도 생성한다.
attack() {
console.log('파이어볼')
}
// 정답: class Wizard { constructor (health, mana, armor) { this.health = health; this.mana = mana; this.armor = armor; } attack() { console.log('파이어볼'); } } const x = new Wizard(545, 210, 10); console.log(x.health, x.mana, x.armor); x.attack();