복습(underbar, class)
1.let a = b || c a에 b를 할당하지만 b가 만약 undefined라면 c를 할당해준다.
2.let a = 'b',
c = 'd' 라는 경우 줄이 바뀌더라도 ','표시가 있기 때문에 같은 let으로 선언된 것으로 해석해야 한다.
3. if (_end === undefined || _end > arr.length) _end = arr.length;
위 함수와 같이 결과가 짧은경우?(모름) 중괄호를 생략해도 되는 것 같다.
4.
_.uniq = function (arr) { // 0 1 1 2
let result = []; //
_.each(arr,function(element) { //each는 [0,1,2,3,1,4,5]배열을 전부 하나씩 돌린다.
for(let i = 0 ; i < result.length ; i++){ //el = 0,1,2,3 1
if(result[i] === element){
return false //@@@@@이부분에서 중요하다 return false를 하니까
} //동작이 멈춰서 아래 push가 기능하지 않는다!
}
result.push(element) //result = [0,1,2,3,]
})
return result
};
5.클래스 및 상속은 아래와 비슷한 방식으로 제작한다.
1단계 = Characters로 캐릭터 틀 제작
class Characters{ //캐릭터 클래스(틀) 생성
constructor(nameValue, atkValue, defValue, hpValue){ //컨스트럭터 내부 이름, 공격력,방어력,체력 설정할 매개변수 입력
this.name = nameValue
this.atk = atkValue ; //3가지 인자 각각 atk, def, hp 할당
this.def = defValue ;
this.hp = hpValue;
}
healHp(heal){
console.log(`${this.name}은 hp를 ${heal}회복했다. ${this.hp} -> ${this.hp + heal}`)
return this.hp += heal
}
loseHp(wound){
console.log(`${this.name}은 ${wound}의 피해를 입었다. ${this.hp} -> ${this.hp - wound}`)
return this.hp -= wound
}
}
2단계 = Secondary_class로 자식생성
class Secondary_class_mage extends Characters{ // 캐릭터에서 상속받은 2차직업 생성
constructor(nameValue, atkValue, defValue, hpValue, mpValue){ // 이름,공격력,방어력,체력,마력 매개변수 받아오기
super() //부모에게서 값 받아오기
this.atk = atkValue;
this.def = defValue;
this.hp = hpValue;
this.mp = mpValue;
}
fireBall(){
if(this.mp>=20) {
console.log(`${this.name}은 fireBall로 50의 피해를 입혔다. ${enemy.hp} -> ${enemy.hp - 50}`)
return this.mp -= 20
}
else{
console.log(`fireBall을 사용하기에는 마나가 ${20 - this.mp}만큼 부족합니다.`
}
}
meditation(){
console.log(`${this.name}은 ${this.mp}의 마나를 회복헀다. ${this.mp} -> ${this.mp + 50}`)
return this.mp += 50
}
}
이런 느낌으로 값 또는 메서드를 추가할 수 있다.
제작하다가 생각해보니 체력과 마력?은 최대치로 할당받은 후 내부값은 최대치가 넘지 않는 선에서 0~최대치까지 이동하는 현재체력/마력값이 필요하고 지력(int)값을 통해 마법(스킬)데미지 증가도 고려할만하다.
물론 게임을 만들려는 것이 아니기 때문에 개선은 없다!
새로운 캐릭터(클래스명이다)를 추가하려면
const userName(이름부분) = new Characters(userName, 20, 10, 100)
//nameValue, atkValue, defValue, hpValue 순서로 만들었다.
//healHp, loseHp메서드를 호출할 수 있어 healHP(30)을 사용하면 30의 hp가 증가한다(아마?)
6. 아래와 같은 방법으로 부모의 메서드를 수정해서 가져올 수 있는 것 같다.
class Lion extends Cat {
speak() {
super.speak();
console.log(`${this.name} roars.`);
}
}
프로미스 등 비동기에 관련된 부분은 월,화 2일간 더 진행될 예정이기 때문에 내용을 같이 정리해야 할 것 같다.
복습하다가 캐릭터 추가에 괜한 시간을 쓴 것 같기도 하다..