Inheritance : Inheritance : Object Oriented JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Object Oriented » Inheritance »

 

Inheritance













Call the constructor from base class

A constructor assigns all properties and methods using the this keyword.

You can make the constructor of BaseClass into a method of SubClass and call it.

After the calling, subClass receives the properties and methods defined in BaseClass's constructor.

In the following code, the 'newMethod' is assigned to BaseClass.

Then, the 'newMethod' is called, passing the color argument from the SubClass constructor.

The final line of code deletes the reference to BaseClass so that it cannot be called later on.








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

function SubClass(sColor) {
    this.newMethod = BaseClass;
    this.newMethod(sColor);
    delete this.newMethod;
}

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







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Object Oriented
» Inheritance