javascript - Overriding inherited prototype method and calling the original one inside the new one -
in following piece of code, how can access a.prototype.log
inside of b.prototype.log
?
function a() {} a.prototype.log = function () { console.log("a"); }; function b() {} b.prototype = object.create(a.prototype); b.prototype.constructor = b; b.prototype.log = function () { //call a.prototype.log here console.log("b"); }; var b = new b(); b.log();
i know write a.prototype.log.call(this)
thought maybe there more elegant way, lets me call in relative way, "call method 'log' of next higher instance in prototype chain". possible?
you use object.getprototypeof
... b.prototype.log = function () { object.getprototypeof (b.prototype).log.call(this) console.log("b"); }; ... b.log(); //a b
note: object.getprototypeof ecmasript 5, see compatibility
there non standard , deprecated __proto__
property (compatibility)
references same object internal [[prototype]]
and allow call a
s' log method this
b.prototype.__proto__.log.call(this)
but
the preferred method use object.getprototypeof.
Comments
Post a Comment