-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance Challenge.js
42 lines (32 loc) · 1.74 KB
/
Inheritance Challenge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 1 - Create a constructor function for a Vehicle. Each vehicle should have a make, model and year property.
// 2 - Add a function to the Vehicle prototype called start which returns the string "VROOM!"
// 3 - Add a function to the Vehicle prototype called toString which returns the string "The make, model, and year are" concatenated with the make, model and year property
/* Examples
var vehicle = new Vehicle("Tractor", "John Deere", 1999)
vehicle.toString() // 'The make, model, and year are Tractor John Deere 1999'
*/
// 4 - Create a constructor function for a Car. Each object created from the Car function should also have a make, model, and year and a property called numWheels which should be 4. The Car prototype should inherit all of the methods from the Vehicle prototype
// 5 - Create a constructor function for a Motorcycle. Each object created from the Motorcycle function should also have a make, model, and year and a property called numWheels which should be 2. The Motorcycle prototype should inherit all of the methods from the Vehicle prototype
function Vehicle(make, model, year){
this.make = make;
this.model = model;
this.year = year;
}
Vehicle.prototype.start = function() {
return "VROOM!";
};
Vehicle.prototype.toString = function(){
return "The make, model, and year are " + this.make + " " + this.model + " " + this.year;
};
function Car(make, model, year){
Vehicle.apply(this, arguments);
this.numWheels = 4;
}
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
function Motorcycle(make, model, year){
Vehicle.apply(this, arguments);
this.numWheels = 2;
}
Motorcycle.prototype = Object.create(Vehicle.prototype);
Motorcycle.prototype.constructor = Motorcycle;