Perché goog.inherits
dalla chiusura Biblioteca di Google simile a questa:Perché creare un costruttore temporaneo quando si esegue l'ereditarietà Javascript?
goog.inherits = function(childCtor, parentCtor) {
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
childCtor.prototype.constructor = childCtor;
};
piuttosto che
goog.inherits = function(childCtor, parentCtor) {
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new parentCtor();
childCtor.prototype.constructor = childCtor;
};
Quali benefici vuol tempCtor
fornire?
Entrambi aggiungono un oggetto inutile nella catena di ereditarietà di istanze di * childCtor *. Nel primo caso, quell'oggetto è un oggetto funzione vuoto. Nel secondo, è un'istanza completa * parentCtor *. Quindi il primo può essere visto come più efficiente (sebbene crei una nuova funzione vuota ogni volta, mentre solo uno è necessario). – RobG
ecco una lettura interessante - http://bolinfest.com/javascript/inheritance.php – dekdev