Shape is inherited by rectangle. This inheritance can be done by many methods. Here I have used apply() and call (). When draw method is child is called, from that method draw method of base class is called again. I have done this thing in two ways. One is making the prototype draw method of base class and the other one is by using apply() and call() method.
First Method :
function Shape () {
this.name='Shape';
this.getName = function () {
return this.name;
};
this.draw = function () {
alert("Something");
};
}
function Rectangle () {
Shape.apply(this);
var X=this.draw;
this.name = 'rectangle';
this.id=500;
this.draw = function () {
X.call(this);
};
}
Second Method :
function Shape () {
this.name='Shape';
this.id=100;
this.getName = function () {
return this.name;
};
}
Shape.prototype.draw = function() {
alert("Something");
};
function Rectangle () {
this.name = 'rectangle';
this.id=200;
this.draw = function () {
Shape.prototype.draw.call(this);
};
}
Rectangle.prototype = new Shape();
Rectangle.prototype.constructor = Rectangle;
Both of these two methods does the similar thing (in case of providing output). I know that by using apply () and call () method I can't directly get the access of base class's prototypes. Inheritance using apply() and call() seems less complicated to me. If both are same then why don't people use apply() and call() that much ? Why do I need to use prototypes ? What problem will I face if I don't use prototypes and inherit base classes using apply() and call () ??