2012-01-24 6 views
3
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;에 함수를 저장하는 것이 좋지 않습니다.

답변

3

현재 구현시 오류가 super_class(this, o);에 있습니다. 당신은 또한 Base_class 자체를 Base_class의 모든 방법을 밀어 및/또는 부모 클래스에 대한 참조를 만들 수 있습니다, 또는

// Basic super class method. 
var Super_class = function(p) { 
    this.init(p); // Call initializer 
}; 

// Define prototype properties and methods 
Super_class.prototype = { 
    constructor: Super_class, 
    init: function(p) { this.p = p + 1; }, 
    method: function(a, b) { 
     console.log("Super class method, arguments: " + [a,b]); 
    } 
}; 

// Define base_class 
var Base_class = function(o) { 
    this.o = o; // Initialize `o` property 
    this.init(o); // Initialize p variable through the initializer 
}; 
// Extend `Base_class` prototype with `method`. 
Base_class.prototype.method = function(a, b) { 
    // Call the method from the parent = Super_class.prototype.method 
    this.constructor.prototype.method(a, b); 
}; 

Base_class.prototype = new Super_class; // Set prototype to `super_class`. 

var bc = new Base_class(0); 
var v1 = bc.o; // 0 
var v2 = bc.p; // 1 
bc.method('Hi: ', [v1, v2]); // Prints "Super class method, arguments: Hi [0,1]" 

:

// Define base_class 
var Base_class = function(o) { 
    var __super__ = this.constructor.prototype; 
    this.o = o; // Initialize `o` property 
    this.init(o); // Initialize p variable through the initializer 
    Base_class.prototype.method = function(a, b) { 
     // Call the method from the parent = Super_class.prototype.method 
     __super__.method(a, b); 
    }; 
}; 
+0

'this.constructor.call (this, o);'는 다중 상속 (둘 이상의 상속)이 실패하게 만듭니다. 'this.constructor (o);'로 충분하다. 'base_class.prototype = new super_class();'는 이미 수퍼 클래스의 모든 속성이 새 클래스에 복사되도록했습니다. –

+0

무례하지 않아야하지만 어떤 식 으로든이 함수는'base_class.method(); '에서'super_class.method();'를 호출하게한다. –

+0

@BrianGraham 편집 된 대답은 * 그 * 질문. 질문의 코드는 실제로 '0과 1'대신에 '0과 NaN'을 출력합니다. 그러므로 이전의 대답. –

1
을 어느 super_class.call(this, o)로 교체하거나 올바르게 초기화 메소드를 구현

http://mckoss.com/jscript/object.htm 링크를 참조하십시오.

다형성 및 하위 클래스 정의에 대한 정보를 찾을 수 있습니다.

1
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(o); // remove "this" 
    this.method = function(a, b) { 
     // call base.method(); 
     // some code 
    } 
} 

base_class.prototype = new super_class(); 

base_class.prototype.constructor = base_class; //important: pointing the constructor back to the base class. 

이것은 JavaScript에서 기본 상속을 수행하는 것입니다. 당신은 멋진 무언가를 얻고 싶은 경우에,

function object(o) { 
    function F() {} 
    F.prototype = o; 
    return new F(); 
} 

자세한 내용은 http://javascript.crockford.com/prototypal.html를 참조하십시오.