2012-12-07 2 views
7

Possible Duplicate:
set attribute with javascript super method- 자바 스크립트

나는 재미를 위해 HTML5의 간단한 게임을 만들려고하고 있습니다. Player 클래스의 수퍼 클래스가되어야하는 Entity 클래스가 있습니다.

function Entity(x, y) { 

    this.x = x; 
    this.y = y; 

    this.tick = function() { 
     //Do generic stuff 
    } 
} 

function Player(x, y) { 

    this.parent.constructor.call(this, x, y); 

    this.tick = function() { 
     //Do player-specific stuff 
     this.parent.tick.call(this); 
    } 
} 

Player.prototype = new Entity(); 
Player.prototype.constructor = Player; 
Player.prototype.parent = Entity.prototype; 

문제는이 라인에있다 : "정의의 '전화'방법을 호출 할 수 없습니다 catch되지 않은 형식 오류 :"

this.parent.tick.call(this); 

내가 크롬의 자바 스크립트 콘솔에 표시되는 오류가 발생합니다.

나는 그것을 얻지 않으며 나는 유사한 문제점의 포스트를 찾아내는 것을 시도했다. 수퍼 클래스의 생성자에 대한 호출이 정상적으로 작동하지만 수퍼 클래스의 틱 메서드에 대한 호출이 작동하지 않습니다.

저는이 게임이 서브 클래스 틱에서 슈퍼 클래스 틱을 호출하는지 잘 모르기 때문에 새로운 게임을 만드는 것에 익숙합니다. 사람들이 사용하는 더 좋고 더 일반적인 방법이 있다면 알려주십시오.

감사합니다.

+0

프로토 타입에 일반 항목이없는 이유는 무엇입니까? 거기에 쉽게 접근 할 수 있습니다. – Bergi

+0

javascript에는 java와 같은 다른 고전 언어에서 사용되는 것처럼 고전적인 상속이 없습니다. 프로토 타입 상속을 읽고 고전적인 상속과의 차이점을 알아야합니다. – Christoph

답변

7

코드에 this answer를 적응 :

function Entity(x, y) { 

    this.x = x; 
    this.y = y; 

    this.tick = function() { 
     //Do generic stuff 
    } 
} 

function Player(x, y) { 

    this.parent.constructor.call(this, x, y); 

    var oldtick = this.tick; 
    this.tick = function() { 
     //Do player-specific stuff 
     oldtick.call(this); 
    } 
} 

Player.prototype = Object.create(Entity.prototype); 
Player.prototype.constructor = Player; 
Player.prototype.parent = Entity.prototype; 
+0

나는 의심이있다. 왜 this를 'this.parent.constructor.call (this, x, y);'에 전달해야할까요? 글쎄,이게 이럴거야. 그러나 나는 이것에 대한 어떤 설명도 찾지 못한다. –

+0

@VeerShrivastav : 그렇지 않으면 생성자가 속성을 설정할 위치를 알 수 없으므로 인스턴스를 전달해야합니다. – Bergi

4

귀하의 질문은 주위를 둘러 보면 나에게 영감을 나는이 개념에 대한 a great article by Josh Gertzen 생각 무엇을 발견했다. 다음 Player tick 및 경고합니다

var Entity = Class.extend({ 
    tick: function() 
    { 
     alert('Entity tick'); 
    } 
}); 

var Player = Entity.extend({ 
    tick: function() 
    { 
     alert('Player tick'); 
     arguments.callee.$.tick.call(this); 
    } 
}); 

p = new Player(); 
p.tick(); 

: 귀하의 경우처럼 간단하다 후

function Class() { } 
Class.prototype.construct = function() {}; 
Class.extend = function(def) 
{ 
    var classDef = function() 
    { 
     if (arguments[0] !== Class) 
     { 
      this.construct.apply(this, arguments); 
     } 
    }; 
    var proto = new this(Class); 
    var superClass = this.prototype; 
    for (var n in def) 
    { 
     var item = def[n];      
     if (item instanceof Function) item.$ = superClass; 
     proto[n] = item; 
    } 
    classDef.prototype = proto; 
    classDef.extend = this.extend;  
    return classDef; 
}; 

:

나는 노골적으로 그의 기사 클래스 상에 extends 방법을 설정하는 몇 가지 코드 복사 Entity tick.

+10

Bergi의 대답은 더 깨끗하지만, 나에게 지나치게 복잡하게 보입니다. :-) 노력의 댓가로 – Bergi

+0

+1. – WTK

+2

이것이 응용 프로그램에서 유일한 수퍼 클래스 호출 인 경우 물론 과장되어 있습니다. 그러나 두 번째 코드 블록이 다른 방법보다 분명하다는 것을 알게되었습니다. 그리고 첫 번째 블록을 한 번 복사하여 붙여 넣기 만하면됩니다. 프로토 타입에 대해 더 이상 생각하지 않아야합니다 :-) – Mark