February 14, 2001 - Defining Duplicate Functions
February 14, 2001 Defining Duplicate Functions Tips: February 2001
Yehuda Shiran, Ph.D.
|
subClass()
defines the method allBye()
which prints an alert box that greets you from subClass()
. The class superClass()
defines the method allBye()
as well, greeting you from superClass()
. The subclass method overrides (by design) the superclass method. When clicking here, you'll get two alert boxes. The first one, alert(newClass.bye())
, demonstrates that the definition of the allBye()
method in subClass()
was not overridden by the later definition inside superClass()
. The second alert box, alert(newClass.hello())
, demonstrates the inheritance of the hello()
method by subClass()
. here are the classes:
function subClass() {
this.inheritFrom = superClass;
this.inheritFrom();
this.bye = allBye;
function allBye() {
return "Bye from subClass";
}
}
function superClass() {
this.bye = allBye;
this.hello = superHello;
function superHello() {
return "Hello from superClass";
}
function allBye() {
return "Bye from superClass";
}
}
var newClass = new subClass();
And the print function is:
function printMethod() {
alert(newClass.bye());
alert(newClass.hello());
}