I am learning javascript inheritance. I found a good explanation here: JavaScript Inheritance
function A() {
B.call(this);
}
function B() {
C.call(this);
this.bbb = function() {
console.log("i was inherited from b!");
}
}
I am trying to implement inheritance based on the above and other solutions (there are a bunch of them online and they all seem to suggest different things). Anyway, I am trying to get SportsCar
to inherit from Car
and use Car's describeSelf
method. I am not sure what I am doing wrong. PLUNK for convenience
var Car = function(make, topSpeed, color){
this.make = make;
this.topSpeed = topSpeed;
this.color = color;
}
Car.prototype.describeSelf = function(){
document.write('Hi, I am a: ' + this.make + ', my top speed is ' + this.topSpeed + ' and I am ' + this.color);
}
var corolla = new Car('toyota', 120, 'blue');
corolla.describeSelf();
//Code works fine up to here
var SportsCar = function(engineSize, make, topSpeed, color) {
Car.call(this, make, topSpeed, color);
this.engineSize = engineSize;
};
var fordGt = new SportsCar('V8', 'ford', 205 , 'red');
fordGt.describeSelf();
I REALLY don't understand what call
does.
Edit: Looks like I wasn't very clear in what I am asking. The essence of the question is make this line work: fordGt.describeSelf();
and get an explanation as to what I am currently doing wrong.