Sto giocando con util.inherits
method from node.js e non riesco a ottenere il comportamento desiderato.util.inherits - come chiamare il metodo di super su istanza?
var util = require("util");
function A() {
this.name = 'old';
}
A.prototype.log = function(){
console.log('my old name is: '+ this.name);
};
function B(){
A.call(this);
this.name = 'new';
}
util.inherits(B, A);
B.prototype.log = function(){
B.super_.prototype.log();
console.log('my new name is: ' + this.name);
}
var b = new B();
b.log();
Il risultato è:
my old name is: undefined
my new name is: new
Tuttavia quello che mi piace è:
my old name is: new
my new name is: new
Che cosa mi manca?
Altro modo: B.super_.prototype.log.call (this); – Klaasvaak
Btw la chiamata è una scelta migliore .. http://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/6 – korCZis
Preferisco usare la parola chiave 'this':' questo .constructor.super_.prototype.log.call (this) ', in modo che non sia necessario utilizzare nuovamente il nome della classe nel metodo – kit