Skip to content

Instantly share code, notes, and snippets.

@neerajkumar
Last active June 11, 2018 16:10
Show Gist options
  • Save neerajkumar/09af1e6eb361280baec71dd1bd73fa68 to your computer and use it in GitHub Desktop.
Save neerajkumar/09af1e6eb361280baec71dd1bd73fa68 to your computer and use it in GitHub Desktop.
ES5 Inheritence Example - Javascript Solution
function Vehicle(speed) {
this.speed = speed;
}
function Car(vehicle_type, speed) {
// this._super.call(this, speed);
this.speed = speed;
this.vehicle_type = vehicle_type;
}
// Car.prototype._super = Vehicle;
function RedCar(color, vehicle_type, speed) {
this._super.call(this, vehicle_type, speed);
this.color = color;
}
RedCar.prototype._super = Car;
RedCar.prototype.desc = function() {
console.log("The Vehicle is of type " + this.vehicle_type + " and its color is " + this.color);
}
RedCar.prototype.drive = function() {
console.log("Vehicle speed is " + this.speed + " km per hour");
}
RedCar.prototype.driveFast = function() {
if (this.speed > 100) {
console.log("Vehicle is driving very fast");
} else {
console.log("Vehicle is driving normally");
}
}
red_car = new RedCar('Red', 'Car', 105);
red_car.desc();
red_car.drive();
red_car.driveFast();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment