관리 메뉴

도드넷

JAVASCRIPT#57 - 자바스크립트 객체 만들기 RECAP 본문

창고/JS KING 포니 [중단]

JAVASCRIPT#57 - 자바스크립트 객체 만들기 RECAP

도드! 2016. 7. 1. 08:45
반응형



I want to be the pony you'd like, you'd love.


JAVASCRIPT#57 - 자바스크립트 함수 RECAP


함수, 객체를 만들어보자.


1. 생성자 함수 만들기


Heros = new Array();


function Hero(character_name, character_class, hp, mp, str, dex, it, attack_dmg, armor, magic_resist, buff, crit)
{
    this.character_name = character_name;
    this.character_class = character_class;
    this.hp = hp;
    this.mp = mp;
    this.str = str;
    this.dex = dex;
    this.it = it;
    this.attack_dmg = attack_dmg;
    this.armor = armor;
    this.magic_resist = magic_resist;
    this.buff = new Array();
    this.crit = 0;
}


* 집합(배열) 만드는방법

- new Array(); 메소드를 이용한다.


2. 생성자 함수를 이용해서 객체 만들기


Dod = new Hero("Dod", "Archer", 300, 200, 10, 5, 6, 12, 5, 7);


Heros.push(Dod);


* 집합에 원소 추가하는 방법

- 집합이름.push(원소); 메소드를 이용한다.


3. 활용하기

- 직업이 궁수면 샤프아이 패시브를 추가한다.

* 샤프아이 : 크리티컬을 2% 증가시키고 총 민첩을 10% 증가시킨다.


for(var i=0; i<Heros.length; i++)
{
    if(Heros[i].character_class == "Archer")
    {
        Heros[i].buff.push("sharp_eye");
    }
}

for(var i=0; i<Heros.length; i++)
{
    for(var j=0; j<Heros[i].buff.length; j++)
    {
        if(Heros[i].buff[j] == "sharp_eye")
        {
            Heros[i].dex = Heros[i].dex + (Heros[i].dex * 0.1);
            Heros[i].crit = Heros[i].crit + 2;
        }
    }
}


for와 length를 이용해서 집합을 전수 검사한다.


4. 메소드 정의하기


Hero.prototype.move = function(direction)
{
    var user = this.character_name;
    alert(user + " is moving to this direction : " + direction);
}

Hero.prototype.attack = function(target)
{
    var user = this.character_name;
    alert(user + " is attacking this object : " + target + "\nDMG : " + this.attack_dmg);  
}


prototype 키워드 메소드를 정의 한다. 

this 키워드는 해당 함수를 사용하는 오브젝트를 지칭한다.










반응형
Comments