February 23, 2001 - Defining Instances
February 23, 2001 Defining Instances Tips: February 2001
Yehuda Shiran, Ph.D.
|
function superClass() {
this.bye = superBye;
this.hello = superHello;
}
function subClass() {
this.bye = subBye;
}
subClass.prototype = new superClass;
function superHello() {
return "Hello from superClass";
}
function superBye() {
return "Bye from superClass";
}
function subBye() {
return "Bye from subClass";
}
var newClass = new subClass();
newClass
is an instance of subClass()
. It was created according to subClass()
's constructor function.
How would the above example look in a class-based language? In Java, it is translated into something like that:
public class superClass {
public superClass () {
this.bye = superBye;
this.hello = superHello;
}
}
public class subClass extends superClass {
public subClass () {
this.bye = subBye;
}
}