5

I have set up a base class as standard:

MyBase = function() {
    this.m_Stuff = 0; // etc
};
MyBase.prototype.MySuperFunction = function (arg1) {
    alert("Hello" + arg1);
};

Next I set up another class that inherits MyBase

MyChild = function () {
    MyBase.call(this);
    this.m_OtherStuff = 1; // etc
};
MyChild.prototype = new MyBase(); // innherit

But then (and this is the bit I dont know how to do) I want to override MyBase's MySuperFunction with a better one, but calling the base class function in the process:

MyChild.prototype.MySuperFunction = function (arg1, arg2) {
    MyBase.MySuperFunction(arg1); // THIS LINE IS THE LINE I DONT KNOW HOW TO DO
    alert("You is a " + arg2 + "'th level idiot");
};

Its a child class that wants to override is base class function, but wants to call the base class function in the new improved definition.

Is this possible, and if so, how can it be done?

Rewind
  • 2,554
  • 3
  • 30
  • 56
  • just to clarify, so are you saying MyBase.prototype.MySuperFunction needs to alert "You is a " + arg2 + "'th level idiot" or you are just trying to invoke it in the context of the child class method? – chrisvillanueva Jun 06 '13 at 13:23
  • I want my child class to call the base class' function before adding it's own amendment. – Rewind Jun 06 '13 at 14:03

2 Answers2

8

It's similar to the call in your inherited constructor. You can access the "super" method still on MyBase.prototype.MySuperFunction (where you assigned it), so use:

MyBase.prototype.MySuperFunction.call(this, arg1);

For a more dynamic approach you even might use Object.getPrototypeOf to get the prototype, but watch out that it works with dynamic inheritance. And if you have many methods that need to call their parent, it can be helpful to alias MyBase.prototype as a super variable which is accessible to all functions on the Child prototype object (see this answer for an example)).

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • So to clarify, you are saying my child's function will be MyChild.prototype.MySuperFunction = function (arg1, arg2) {MyBase.prototype.MySuperFunction.call(this, arg1); alert("You is a " + arg2 + "'th level idiot"); }; – Rewind Jun 06 '13 at 14:06
  • @Bergi is there a way to call this prototype thing *within* the original declaration of the function/object? this [gist](https://gist.github.com/abbood/10108937) provides an example of what i'm talking about – abbood Apr 08 '14 at 10:48
  • No, prototype properties have to be declared outside the constructor function – Bergi Apr 08 '14 at 10:53
  • @Bergi!!!!!!!! that's incredibly ugly isn't it? I mean I would like to know that everything related to a defined "object" is encapsulated within those braces.. otherwise anyone can add all sortsa stuff anywhere within 100 js files in a large project.. is there a way to address this? – abbood Apr 08 '14 at 11:10
  • Yes, it's called *module pattern* - basically wrap constructor and prototype assignments in an IEFE. See also [this question](http://stackoverflow.com/q/21296559/1048572). Btw, you don't need to shout at me :-) – Bergi Apr 08 '14 at 11:54
  • You really should ask that as a new question, maybe at [codereview.SE]. For now I can say that you're at least missing `Class.prototype.constructor = Class` – Bergi Apr 08 '14 at 13:35
  • woah.. never heard of codereview before.. how's it really different from SO? – abbood Apr 08 '14 at 13:56
  • Basically you ask about working code (and how to improve it), not about problems (and how to solve them). http://codereview.stackexchange.com/tour – Bergi Apr 08 '14 at 14:33
3

Please apply the following:-

MyBase.prototype.MySuperFunction.call(this, arg1);
pvnarula
  • 2,771
  • 18
  • 22
  • So to clarify, you are saying my child's function will be MyChild.prototype.MySuperFunction = function (arg1, arg2) {MyBase.prototype.MySuperFunction.call(this, arg1); alert("You is a " + arg2 + "'th level idiot"); }; Also, when I set up an instance elsewhere; var mychild = new MyChild(); I would then call the CHILD'S version of the function with: mychild.MySuperFunction("Dave", "7"); – Rewind Jun 06 '13 at 14:07