Sto cercando di implementare l'eredità con il modello del modulo in questo modo:Ereditarietà e modulo modello
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");
}
}
};
ma non riesco a chiamare da esempio per figli test2()
.
var c = new Child();
c.test2(); // c.test2 is not a function
Cosa ho sbagliato?
Per i principianti, 'this.prototype' all'interno di un costruttore non farà ciò che si pensa. Ti suggerisco di cercare un tutorial. –
[Come implementare l'ereditarietà nel modello di prototipo JS Revealing?] (Http://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern) spiega in dettaglio come usa il modello del modulo e l'ereditarietà. – Bergi