Open richterd opened 5 years ago
Yeah, my inheritance method is not well suited to overrides. Also, technically, since the sub class recreates the prototype of the super class it isn't true prototype inheritance if you really think about it, just prototype cloning. I generally have to define any method overrides after defining the prototype by doing: subclass.prototype.inheritedMethod = function () {}; Which is basically just replacing the method I copied from the super's prototype. I really like your implementation, though. What is that syntax you use to define the subclass's prototype at the end using a comma? I will test it out when I have some time.
On Fri, Dec 14, 2018, 5:22 AM Daniel Richter <notifications@github.com wrote:
Hi, thank you for the Tutorial on the Platformer Game ( https://github.com/frankarendpoth/frankarendpoth.github.io/tree/master/content/pop-vlog/javascript/2018/006-rabbit-trap). I really enjoyed watching it. While implementing an own version of it, I recognized an issue with how you implement the "class inheritance".
The way you do it is the following:
Superclass = function(){ //Definition of Superclass attributes }Superclass.prototype = { //Definition of Superclass methods }Subclass = function(){ Superclass.call(this); //Definition of Subclass attributes }Subclass.prototype = { //Definition of Subclass methods }Object.assign(Subclass.prototype, Superclass.prototype);Subclass.prototype.constructor = Subclass;
This seems to work for your approach, but as soon as you want to override methods, your implementation leads to always calling the superclass' method instead of the subclass one (Similar to the constructor, that you also had to fix after the assignment).
A nicer way of implementing your approach, and also have the correct overriding behavior would be to turn around the assignment and e.g. use the following instead:
Superclass = function(){ //Definition of Superclass attributes }Superclass.prototype = { //Definition of Superclass methods }Subclass = function(){ Superclass.call(this); //Definition of Subclass attributes }Subclass.prototype = Object.assign(Object.create(Superclass.prototype), { //Definition of Subclass methods });
That way, the subclass' prototype methods correctly overwrite the Superclass once. Method overriding works as intended. No need to fix constructors.
Best, Daniel
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/frankarendpoth/frankarendpoth.github.io/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/APcmwoD4Y3Ig6yqenw9wG-nc8rcJC6idks5u43vOgaJpZM4ZTQkV .
I'm sorry, I'm reading this on a phone and I misread your code. Yours is much cleaner and I will definitely test it out. I will probably make a correction video as well. Excellent catch!
On Fri, Dec 14, 2018, 6:30 AM Frank Poth <frankarendpoth@gmail.com wrote:
Yeah, my inheritance method is not well suited to overrides. Also, technically, since the sub class recreates the prototype of the super class it isn't true prototype inheritance if you really think about it, just prototype cloning. I generally have to define any method overrides after defining the prototype by doing: subclass.prototype.inheritedMethod = function () {}; Which is basically just replacing the method I copied from the super's prototype. I really like your implementation, though. What is that syntax you use to define the subclass's prototype at the end using a comma? I will test it out when I have some time.
On Fri, Dec 14, 2018, 5:22 AM Daniel Richter <notifications@github.com wrote:
Hi, thank you for the Tutorial on the Platformer Game ( https://github.com/frankarendpoth/frankarendpoth.github.io/tree/master/content/pop-vlog/javascript/2018/006-rabbit-trap). I really enjoyed watching it. While implementing an own version of it, I recognized an issue with how you implement the "class inheritance".
The way you do it is the following:
Superclass = function(){ //Definition of Superclass attributes }Superclass.prototype = { //Definition of Superclass methods }Subclass = function(){ Superclass.call(this); //Definition of Subclass attributes }Subclass.prototype = { //Definition of Subclass methods }Object.assign(Subclass.prototype, Superclass.prototype);Subclass.prototype.constructor = Subclass;
This seems to work for your approach, but as soon as you want to override methods, your implementation leads to always calling the superclass' method instead of the subclass one (Similar to the constructor, that you also had to fix after the assignment).
A nicer way of implementing your approach, and also have the correct overriding behavior would be to turn around the assignment and e.g. use the following instead:
Superclass = function(){ //Definition of Superclass attributes }Superclass.prototype = { //Definition of Superclass methods }Subclass = function(){ Superclass.call(this); //Definition of Subclass attributes }Subclass.prototype = Object.assign(Object.create(Superclass.prototype), { //Definition of Subclass methods });
That way, the subclass' prototype methods correctly overwrite the Superclass once. Method overriding works as intended. No need to fix constructors.
Best, Daniel
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/frankarendpoth/frankarendpoth.github.io/issues/5, or mute the thread https://github.com/notifications/unsubscribe-auth/APcmwoD4Y3Ig6yqenw9wG-nc8rcJC6idks5u43vOgaJpZM4ZTQkV .
Hi, thank you for the Tutorial on the Platformer Game (https://github.com/frankarendpoth/frankarendpoth.github.io/tree/master/content/pop-vlog/javascript/2018/006-rabbit-trap). I really enjoyed watching it. While implementing an own version of it, I recognized an issue with how you implement the "class inheritance".
The way you do it is the following:
This seems to work for your approach, but as soon as you want to override methods, your implementation leads to always calling the superclass' method instead of the subclass one (Similar to the constructor, that you also had to fix after the assignment).
A nicer way of implementing your approach, and also have the correct overriding behavior would be to turn around the assignment and e.g. use the following instead:
That way, the subclass' prototype methods correctly overwrite the Superclass once. Method overriding works as intended. No need to fix constructors.
Best, Daniel