Gram z util.inherits
method from node.js i nie mogę uzyskać pożądanego zachowania.util.inherits - jak wywołać metodę super na instancji?
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();
Wynikiem jest:
my old name is: undefined
my new name is: new
Jednak to, co chciałbym to:
my old name is: new
my new name is: new
Czego mi brakuje?
Inny sposób: B.super_.prototype.log.call (this); – Klaasvaak
Przyjemniejsze połączenie jest lepszym wyborem. Http://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-ind/6 – korCZis
Wolę używać słowa kluczowego "this": 'this .constructor.super_.prototype.log.call (this) ', dzięki czemu nie trzeba używać nazwy klasy ponownie w metodzie – kit