faqts : Computers : Programming : Languages : JavaScript : Language Core : prototypes/inheritance

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 12 people (50%) answered Yes
Recently 3 of 9 people (33%) answered Yes

Entry

Another way to have (call) a super-mathod

Jul 25th, 2000 01:31
Wolfram Kriesing,


I was searching on how to call a super method, which means calling the 
method of the parent class, which i extend in it's child class.
I was reading through the article of Martin Honnen and i was quite 
frightened to see, that it is so "complicated" and browser specific 
to call a super class. And while trying to use it I came up with another
IMO easier idea. It does not really look like calling the super class,
but anyway it works exactly as I (at least) expect it to work.
In words: simply define a method, with a different name in the parent 
class, which also only calls the parent-method of the method u want
to extend in a child class, and call it in the place where the super
method should be called.
In real it looks like that (copy/paste and u can try it):

function class_parent()
{
  this.theMethod = parent_theMethod;
  // define the method again, to use as the super method
  this.parent_theMethod = parent_theMethod; 
}

function parent_theMethod()
{
  alert("this is the parent class");
}

////////////////////////////////////////////////////////////////////////

function class_child()
{
  this.theMethod = child_theMethod;
}

function child_theMethod()
{
  alert("this is the child class");
  this.parent_theMethod(); // call the parent's method, the super method
}
class_child.prototype = new class_parent;

myChild = new class_child;
myChild.theMethod();


suggestions, hints or any comments on that, i would love to 
receive, because i guess there are some experts out there, i would like
to hear their opinion, too...

Wolfram Kriesing