객체 생성자 만들기
//human 이라는 클래스 객체를 생성 const human = function (name,hp,pow){ //this가 가르키는 것은 이 객체를 의미 //이 객체의 속성 this.name = name; this.hp = hp; this.pow = pow; //이 객체의 메소드 //target은 공격을 당하는 human 클래스의 다른 인스턴스 this.attack = function(target){ console.log(this.name+"이 "+target.name+"을 공격했다."); target.hp -= this.pow; }; this.show = function(){ console.log("[%s] %d", this.name,this.hp); }; } -----human 클래스의 인스턴스 객체를 생성하기 //객체 생성자로 만들어둔 것을 이용해 인스턴스를 생성 var p1 = new human("혜원",200,40) p1 = {name : "혜원", hp : 200 , pow : 40} var p2 =. new human("동윤",300,80) p2 = {name : "동윤", hp : 300 , pow : 80} -----객체의 메소드 이용하기 //인트턴스.메소드(타겟 인스턴스) p1.attack(p2) -콘솔- 혜원이 동윤을 공격했다. p2 = {name : "동윤", hp : 260 , pow : 80}
p1,p2는 human클래스의 인스턴스 객체이다.