Hybrid Method for class inheritance : Inheritance : Object Oriented JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Object Oriented » Inheritance »

 

Hybrid Method for class inheritance








In the SubClass constructor, object masquerading is used to inherit the color property from BaseClass.

Then prototype chaining is used to inherit the methods of BaseClass.








function BaseClass(sColor) {
    this.color = sColor;
}

BaseClass.prototype.sayColor = function () {
    alert(this.color);
};

function SubClass(sColor, sName) {

    BaseClass.call(this, sColor);
    this.name = sName;
}

SubClass.prototype = new BaseClass();

SubClass.prototype.sayName = function () {
    alert(this.name);
};

var objA = new BaseClass("red");
var objB = new SubClass("blue""MyName");
objA.sayColor();
objB.sayColor();
objB.sayName();







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Object Oriented
» Inheritance