0

So I have cerated a class

function myClass()
{
}

With the method

myClass.prototype.theMethod=function()
{
}

Which is fine but I have a circumstance where I need to use this class but add extra commands to method instead of just overwriting the whole thing, if that is possible?

Guesser
  • 1,769
  • 3
  • 25
  • 52
  • 1
    Can you be more precise ? Do you want to override this function for an instance ? For a subclass ? – Denys Séguret Sep 06 '13 at 10:31
  • No as I said i just want to add extra commands to this method even if that was just a single line to call another function. – Guesser Sep 06 '13 at 10:33
  • Been discussed before http://stackoverflow.com/questions/560829/calling-base-method-using-javascript-prototype and no, there's no `super` or `parent` in JavaScript. – Sergiu Paraschiv Sep 06 '13 at 10:33

2 Answers2

0

If you need to overrid action in certain object, then you can do the following:

var myInstance = new myClass();

myInstance.theMethod = function () {
    // do additional stuff
    // now call parent method:
    return myClass.prototype.theMethod.apply(this, arguments);
}

For subclasses solution is almost the same, but you perform that not on instance, but on prototype of inherited "class".

kirilloid
  • 14,011
  • 6
  • 38
  • 52
0

Like this:

var theOldMethod = myClass.prototype.theMethod;
myClass.prototype.theMethod=function()
{
    //Do stuff here
    var result = theOldMethod.apply(this, arguments);
    //Or here
    return result;
}
Zotta
  • 2,513
  • 1
  • 21
  • 27