admin管理员组文章数量:1026989
In the following piece of code, how can I access the 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 I could just write A.prototype.log.call(this)
but I thought maybe there is a more elegant way, that lets me call it in a relative way, like "call the method 'log' of the next higher instance in the prototype chain". Is something like this possible?
In the following piece of code, how can I access the 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 I could just write A.prototype.log.call(this)
but I thought maybe there is a more elegant way, that lets me call it in a relative way, like "call the method 'log' of the next higher instance in the prototype chain". Is something like this possible?
-
1
actually,
A.prototype.log.call(this)
was exactly what I searched for. Thank you! – ProblemsOfSumit Commented Nov 18, 2014 at 9:30
1 Answer
Reset to default 6You could use Object.getPrototypeOf
...
B.prototype.log = function () {
Object.getPrototypeOf (B.prototype).log.call(this)
console.log("B");
};
...
b.log(); //A B
Note: Object.getPrototypeOf is ECMASript 5, see the patibility
There is also the non standard and deprecated __proto__
property (patibility) which
references the same object as its internal [[Prototype]]
and would allow you to call your A
s' log method like this
B.prototype.__proto__.log.call(this)
But
the preferred method is to use Object.getPrototypeOf.
In the following piece of code, how can I access the 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 I could just write A.prototype.log.call(this)
but I thought maybe there is a more elegant way, that lets me call it in a relative way, like "call the method 'log' of the next higher instance in the prototype chain". Is something like this possible?
In the following piece of code, how can I access the 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 I could just write A.prototype.log.call(this)
but I thought maybe there is a more elegant way, that lets me call it in a relative way, like "call the method 'log' of the next higher instance in the prototype chain". Is something like this possible?
-
1
actually,
A.prototype.log.call(this)
was exactly what I searched for. Thank you! – ProblemsOfSumit Commented Nov 18, 2014 at 9:30
1 Answer
Reset to default 6You could use Object.getPrototypeOf
...
B.prototype.log = function () {
Object.getPrototypeOf (B.prototype).log.call(this)
console.log("B");
};
...
b.log(); //A B
Note: Object.getPrototypeOf is ECMASript 5, see the patibility
There is also the non standard and deprecated __proto__
property (patibility) which
references the same object as its internal [[Prototype]]
and would allow you to call your A
s' log method like this
B.prototype.__proto__.log.call(this)
But
the preferred method is to use Object.getPrototypeOf.
本文标签:
版权声明:本文标题:javascript - Overriding inherited prototype method and calling the original one inside the new one - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662911a2162001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论