Mega Code Archive

 
Categories / JavaScript Tutorial / Object Oriented
 

Using apply method to call the constructor of the base class

function BaseClass(sColor) {     this.color = sColor;     this.sayColor = function () {         alert(this.color);     }; } function SubClass(sColor, sName) {     BaseClass.apply(this, new Array(sColor));     this.name = sName;     this.sayName = function () {         alert(this.name);     }; } var objA = new BaseClass("red"); var objB = new SubClass("blue", "MyName"); objA.sayColor(); objB.sayColor(); objB.sayName();