This is the extend function that creates the superclass and subclass relationship.
function extend(subclass, superclass) {
function Dummy(){}
Dummy.prototype = superclass.prototype;
subclass.prototype = new Dummy();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass;
subclass.superproto = superclass.prototype;
}
Here Person is Super Class. Its toString function is set like thisfunction() {
return this.first + ' ' + this.last;
};
////////////////////Super Class////////////////References:function Person(first, last) {
Now we will create a subclass named Employee
this.first = first;
this.last = last;
}
Person.prototype.toString = function() {
return this.first + ' ' + this.last;
};////////////////////Sub Class////////////////
function Employee(first, last, id) {
Employee.superclass.call(this, first, last);
this.id = id;
}
extend(Employee, Person);
Employee.prototype.toString = function() {
return Employee.superproto.toString.call(this) + ': ' + this.id;
};John Dough
Output of the html will be like this
Bill Joi: 100
http://kevlindev.com/tutorials/javascript/inheritance/index.htm
Author:
Kazi Masudul Alam
Software Engineer