var super_class = function(p) {
this.p = p + 1;
this.method = function(a, b) {
// some code
};
};
var base_class = function(o) {
this.o = o;
super_class.call(this, o);
this.method = function(a, b) {
// call super_class .method();
// some code
}
}
base_class.prototype = new super_class();
var bc = new base_class(0);
var v1 = bc.o; // 0
var v2 = bc.p; // 1
에서 전화 기본 기능은 어떻게 super_class method
를 호출 할 수 있습니다. 이름을 변경하면 다른 기능에서 this.method(3, 4);
으로 전화하면됩니다. 다른 확장 클래스에 확장 클래스를 만들므로 함수 이름을 변경하면 도움이되지 않습니다.자바 스크립트 : 이름과 특성 <strong>가 동일 할</strong>을 가정 할 때 프로토 타입 상속
또한 개인 변수 var pmethod = this.method;
에 함수를 저장하는 것이 좋지 않습니다.
'this.constructor.call (this, o);'는 다중 상속 (둘 이상의 상속)이 실패하게 만듭니다. 'this.constructor (o);'로 충분하다. 'base_class.prototype = new super_class();'는 이미 수퍼 클래스의 모든 속성이 새 클래스에 복사되도록했습니다. –
무례하지 않아야하지만 어떤 식 으로든이 함수는'base_class.method(); '에서'super_class.method();'를 호출하게한다. –
@BrianGraham 편집 된 대답은 * 그 * 질문. 질문의 코드는 실제로 '0과 1'대신에 '0과 NaN'을 출력합니다. 그러므로 이전의 대답. –