最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Overriding inherited prototype method and calling the original one inside the new one - Stack Overflow

matteradmin6PV0评论

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?

Share Improve this question asked Jul 25, 2013 at 12:55 basilikumbasilikum 10.5k5 gold badges49 silver badges58 bronze badges 1
  • 1 actually, A.prototype.log.call(this) was exactly what I searched for. Thank you! – ProblemsOfSumit Commented Nov 18, 2014 at 9:30
Add a ment  | 

1 Answer 1

Reset to default 6

You 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 As' log method like this

B.prototype.__proto__.log.call(this)

But

the preferred method is to use Object.getPrototypeOf.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far