3

私は数日間javascriptでoopと戦っていますが、解決策が見つかりません.

私は 3 つのオブジェクト、スーパー クラス、子クラス、および子クラスですべての「プロトタイプ」マジックを実行する必要がある inheritance_manager を作成しました。

継承マネージャー:

Inheritance_Manager = {};
Inheritance_Manager.extend = function(subClass, baseClass) {
    function inheritance() {
    }
    inheritance.prototype = baseClass.prototype;
    subClass.prototype = new inheritance();
    subClass.prototype.constructor = subClass;
    subClass.baseConstructor = baseClass;
    subClass.superClass = baseClass.prototype;
};

スーパー (親) クラス:

SDSection = function(sectionId, resourceId) {

    this.self = this;
    this.sectionId = sectionId;
    this.resourceId = resourceId;

    ...
};

SDSection.prototype.doSetups = function() {
        ...
};

子クラス:

TypedSectionHandler = function(sectionId, resourceId) {
    SDSection.call(this, sectionId, resourceId);
        ...
};

Inheritance_Manager.extend(TypedSectionHandler, SDSection);

TypedSectionHandler.prototype.doSetups = function() {
        ...
    SDSection.doSetups.call(this);
        ...
};

私がやりたいことは、phpやjavaなどの他のプログラミング言語で簡単です。タイプ「TypedSectionHandler」の子クラスのメソッド「doSetups」から、親クラス「SDSection」の上書きされた「doSetups」メソッドを呼び出したい。

私はこの問題に約1週間苦労しており、基本的なものからより複雑なものまでさまざまな解決策を試しましたが、何もうまくいかないようです.

たとえば、クロムやFirefoxでスクリプトが実行されるたびに、「未定義のメソッド呼び出しを呼び出すことができません」というエラーが表示されるか、より単純な「SDSection.doSetups」が定義されていません。

少なくとも私はここから上記のアプローチを採用し、それを自分のニーズに合わせましたが、とにかく機能せず、ブラウザーは同じエラーで終了しています。ゆっくりと私は真剣に気が狂い始めています。:)

誰かが私が間違っていることと、機能するソリューションがどのように見えるかを知っていますか?

ありがとう

ウド

4

3 に答える 3

4

doSetups親クラスの の一部である関数を呼び出す必要がありますprototype

TypedSectionHandler.prototype.doSetups = function() {
        ...
    SDSection.prototype.doSetups.call(this);
        ...
};

継承のさまざまな手法 (特に、呼び出し元が親の型を知る必要がない手法) を確認したい場合は、CoffeeScript の__extends関数と、それが継承を実行する方法を確認することをお勧めします。

于 2013-01-24T22:24:34.770 に答える
3

純粋なJavaScriptを実行している場合は、voithosのソリューションを使用してください。現在のプロジェクトでは、 JohnResigの単純な継承スニペットを使用するのが好きです。縮小後はわずか521バイトで、依存関係はありません。この場合、次のように機能します。

SDSection = Class.extend({
    init: function(sectionId, resourceId) {

        this.sectionId = sectionId;
        this.resourceId = resourceId;

        ...
    },
    doSetups: function(){
        ...
    }
});

TypedSectionHandler = SDSection.extend({
    doSetups: function(){
        ...
        this._super();
        ...
    }
});
于 2013-01-24T22:35:37.250 に答える
0

自分でやりたい場合は、voithosが言ったように行くことができます-自分で書くと、js OOPを確実に理解するのに役立ちます。

もっと快適にしたい場合(= applyandを使用するcall必要がない場合 - Visual Studio 2012 で開発している場合 - base-methods/-constructor intellisense support ) 私が書いたフレームワークをチェックアウトしてください: jsframework

それを使用すると、サンプルを非常に簡単に作成できます。

SDSection = new Class(Object, function(base, baseConstructor)
{
    // member fields for prototype
    this.self = null;
    this.sectionId = -1;
    this.resourceId = -1;

    // constructor
    this.init = function(sectionId, resourceId)
    {
        this.self = this;
        this.sectionId = sectionId;
        this.resourceId = resourceId;
    };

    // member functions
    this.doSetups = function() 
    {
        console.log("SDSection doSetups: " + this.sectionId + "/" + this.resourceId);
    };
});

TypedSectionHandler = new Class(SDSection, function(base, baseConstructor)
{
    this.anotherId = -2;

    // constructor
    this.init = function(sectionId, resourceId, anotherId) 
    {
        baseConstructor(sectionId, resourceId);

        this.anotherId = anotherId;
    };

    // member functions
    this.doSetups = function() 
    {
        // call base function
        base(this).doSetups();
        console.log("TypedSectionHandler doSetups: " + this.anotherId);
    };
});

var t = new TypedSectionHandler(1, 2, 3);
t.doSetups();
console.log(t instanceof TypedSectionHandler);
console.log(t instanceof SDSection);

ここに動作するjsfiddleがあります:http://jsfiddle.net/QYXSr/1/

出力:

SDSection doSetups: 1/2
TypedSectionHandler doSetups: 3
true
true 

免責事項:

私が言ったように、私はこのフレームワークの開発者です ;)

于 2013-01-24T23:14:54.963 に答える