2013-03-15 14 views
7

私はこのようにモジュールパターンと継承を実装しようとしています:継承とモジュールパターン

Parent = function() { 

    //constructor 
    (function construct() { 
     console.log("Parent"); 
    })(); 

    // public functions 
    return this.prototype = { 

     test: function() { 
      console.log("test parent"); 
     }, 


     test2: function() { 
      console.log("test2 parent"); 
     } 

    }; 
}; 


Child = function() { 

    // constructor 
    (function() { 
     console.log("Child"); 
     Parent.call(this, arguments); 
     this.prototype = Object.create(Parent.prototype); 
    })(); 


    // public functions 
    return this.prototype = { 

     test: function() 
     { 
      console.log("test Child"); 
     } 

    } 

}; 

が、私は子供のインスタンスtest2()から呼び出すことができません。

var c = new Child(); 
c.test2(); // c.test2 is not a function 

何が間違っていますか?

+3

まず、コンストラクタ内の 'this.prototype'は、あなたの考えをしません。チュートリアルを参照することをお勧めします。 –

+1

[JSで継承を実装する方法は、プロトタイプパターンを公開していますか?](http://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern)モジュールパターンと継承を使用します。 – Bergi

答えて

11

正しいパターンでモジュールパターンを使用していません。どういうわけか、あなたの "コンストラクタ"が直ちに呼び出される関数式(IIFE)として呼び出され、モジュールクロージャは呼び出されません。それはもう一方の方法でなければなりません。

また、this.prototypeに割り当てることはできません。すべてのインスタンスが継承するプロトタイプオブジェクトを作成するには、コンストラクタ関数this keywordの場合はグローバルwindowオブジェクトを指す場合もあります)のプロパティをprototypeに割り当てる必要があります。

そして、プロトタイプオブジェクトではなく、コンストラクタ関数をすぐにIIFEから戻す必要があります。

Parent = (function() { 
    // constructor 
    function construct() { 
     console.log("Parent"); 
    }; 

    // public functions 
    construct.prototype.test = function() { 
     console.log("test parent"); 
    }; 
    construct.prototype.test2 = function() { 
     console.log("test2 parent"); 
    }; 

    return construct; 
})(); 


Child = (function() { 
    // constructor 
    function construct() { 
     console.log("Child"); 
     Parent.apply(this, arguments); 
    } 

    // make the prototype object inherit from the Parent's one 
    construct.prototype = Object.create(Parent.prototype); 
    // public functions 
    construct.prototype.test = function() { 
     console.log("test Child"); 
    }; 

    return construct; 
})(); 
+0

ありがとうございます。今私は別の質問があるでしょう:それは静的関数を子供に追加する可能性はありますか? – Webman

+1

"静的"という意味に依存して、ダイナミックJS言語のそれと実質的に同等です:-)しかし、あなたは 'Child'コンストラクタ関数オブジェクトに関数を割り当てることができます:' Child.method = function(){ ...}; '(またはモジュールクロージャの中で、' construct.method = ...; ') – Bergi

+0

は「静的」とは、' Child'という名前をつけずに関数を呼び出すことを意味します。このようなものChild.myStaticFunction() – Webman

0
(function() { 
    console.log("Child"); 
    Parent.call(this, arguments); 
    this.prototype = Object.create(Parent.prototype); 
})(); 

thisあなたが関数にコードを巻いているため、windowを指します。ラッピング関数を削除するか、引数としてthisを渡します。

+0

外側の 'this'を' this'として渡すことも間違いです。 – Bergi

+0

「this」を 'this 'として渡すのは誰ですか? – zeroflagL

+0

あなたは "*これを引数として渡す"と言っていますが、これは役に立たないようです。 OK、私はあなたがthisArgとして渡したと仮定していましたが、そうでない場合でも問題はありません - 考えたコードを投稿してください。 – Bergi