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?

2
  • 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? Commented Jun 6, 2013 at 13:23
  • I want my child class to call the base class' function before adding it's own amendment. Commented Jun 6, 2013 at 14:03

2 Answers 2

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)).

Sign up to request clarification or add additional context in comments.

8 Comments

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"); };
@Bergi is there a way to call this prototype thing within the original declaration of the function/object? this gist provides an example of what i'm talking about
No, prototype properties have to be declared outside the constructor function
@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?
Yes, it's called module pattern - basically wrap constructor and prototype assignments in an IEFE. See also this question. Btw, you don't need to shout at me :-)
|
3

Please apply the following:-

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

1 Comment

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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.