admin管理员组文章数量:1356470
I'm trying to create a simple inheritance structure using the class syntax in ES6. I have a parent class with a method, say update()
, and a child class which also needs an update()
method. I would like a call to child.update()
to contain a call to parent.update()
, as well as containing some additional functionality specific to the child class.
I find that creating this method in the child class seems to overwrite the reference to that method in the parent class, meaning I can't call both.
Here is an example:
class Xmover {
constructor(x, speedX) {
this.x = x;
this.speedX = speedX;
}
update() {
this.x += this.speedX;
}
}
class XYmover extends Xmover {
constructor(x, y, speedX, speedY) {
super(x, speedX);
this.y = y;
this.speedY = speedY;
}
update() {
this.y += this.speedY;
// *** I would like this to also update the x position with a call
// to Xmover.update() so I don't have to repeat the code ***
}
}
testXY = new XYmover(0, 0, 10, 10);
console.log(`Start pos: ${textXY.x}, ${testXY.y}`);
testXY.update();
console.log(`End pos: ${textXY.x}, ${testXY.y}`);
This produces the output:
Start pos: 0, 0
End pos: 0, 10
As you can see, the y position is correctly updated with the call to XYmover.update()
, but this new definition overrides any calls to Xmover.update()
. If both functions were being called, we'd expect to see an end position of 10, 10
.
I have seen people who don't use this class syntax get around this by creating a copy of the original super function in a way similar to this:
var super_update = this.update;
update() {
// ... other functionality
super_update();
}
However, this doesn't feel ideal to me, and it also doesn't work with the class syntax (unless you define this super_update
in the child class constructor, which seems like it would create other issues of having a full copy of the parent.update()
function in each instance of the child object).
I am new to Javascript so I haven't fully gotten my head around the mechanics of using prototypes yet - perhaps the best solution involves these somehow? At my level of understanding, these would work similarly however, in that even if a prototype function was defined, creating a function with that name would mean that the prototype never gets called either.
I'm trying to create a simple inheritance structure using the class syntax in ES6. I have a parent class with a method, say update()
, and a child class which also needs an update()
method. I would like a call to child.update()
to contain a call to parent.update()
, as well as containing some additional functionality specific to the child class.
I find that creating this method in the child class seems to overwrite the reference to that method in the parent class, meaning I can't call both.
Here is an example:
class Xmover {
constructor(x, speedX) {
this.x = x;
this.speedX = speedX;
}
update() {
this.x += this.speedX;
}
}
class XYmover extends Xmover {
constructor(x, y, speedX, speedY) {
super(x, speedX);
this.y = y;
this.speedY = speedY;
}
update() {
this.y += this.speedY;
// *** I would like this to also update the x position with a call
// to Xmover.update() so I don't have to repeat the code ***
}
}
testXY = new XYmover(0, 0, 10, 10);
console.log(`Start pos: ${textXY.x}, ${testXY.y}`);
testXY.update();
console.log(`End pos: ${textXY.x}, ${testXY.y}`);
This produces the output:
Start pos: 0, 0
End pos: 0, 10
As you can see, the y position is correctly updated with the call to XYmover.update()
, but this new definition overrides any calls to Xmover.update()
. If both functions were being called, we'd expect to see an end position of 10, 10
.
I have seen people who don't use this class syntax get around this by creating a copy of the original super function in a way similar to this:
var super_update = this.update;
update() {
// ... other functionality
super_update();
}
However, this doesn't feel ideal to me, and it also doesn't work with the class syntax (unless you define this super_update
in the child class constructor, which seems like it would create other issues of having a full copy of the parent.update()
function in each instance of the child object).
I am new to Javascript so I haven't fully gotten my head around the mechanics of using prototypes yet - perhaps the best solution involves these somehow? At my level of understanding, these would work similarly however, in that even if a prototype function was defined, creating a function with that name would mean that the prototype never gets called either.
Share Improve this question asked Apr 28, 2019 at 14:47 DylanJaideDylanJaide 4051 gold badge5 silver badges13 bronze badges 01 Answer
Reset to default 9 super.update();
isn't that super? That will look up the update
function in the superclass, and call it with the correct this
.
版权声明:本文标题:javascript - How to call a parent class method inside a child class method of the same name? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744011992a2575737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论