私はこのようにモジュールパターンと継承を実装しようとしています:継承とモジュールパターン
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
何が間違っていますか?
まず、コンストラクタ内の 'this.prototype'は、あなたの考えをしません。チュートリアルを参照することをお勧めします。 –
[JSで継承を実装する方法は、プロトタイプパターンを公開していますか?](http://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern)モジュールパターンと継承を使用します。 – Bergi