1. Create a constructor function named Car that will accept the following parameters and set them to properties of the same name. (Hint: Use the keyword this ). make model year color 2. Inside the constructor function set the property kmTravelled a value of 0. 3. Create a function on the Car prototype named drive . This function will accept a parameter named distance and inside the function will update the kmTravelled property on our object by adding the passed in value of distance. 4. Add a friendly console message to the drive method displaying the vehicle’s year, make, model and distance travelled. 5. Use the Car constructor function to create a new instance of Car and save it to a variable. Pass it values of a car you own or would like to own. 6. Create another Car object and save it to a variable. Set the values to a car that belongs to a family member or friend. 7. Execute the drive method a few times on your newly created cars (don’t forget to pass a distance). Sample output: The 1969 Ford Mustang has travelled 123km. The 2014 Chevy Cruze has travelled 112km. The 2014 Chevy Cruze has travelled 155km. The 1969 Ford Mustang has travelled 453km. The 1969 Ford Mustang has travelled 698km. The 2014 Chevy Cruze has travelled 598km.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
1. Create a constructor function named Car that will accept the following parameters and set them to properties of
the same name. (Hint: Use the keyword this ).
make
model
year
color
2. Inside the constructor function set the property kmTravelled a value of 0.
3. Create a function on the Car prototype named drive . This function will accept a parameter named distance
and inside the function will update the kmTravelled property on our object by adding the passed in value of
distance.
4. Add a friendly console message to the drive method displaying the vehicle’s year, make, model and distance
travelled.
5. Use the Car constructor function to create a new instance of Car and save it to a variable. Pass it values of a car
you own or would like to own.
6. Create another Car object and save it to a variable. Set the values to a car that belongs to a family member or
friend.
7. Execute the drive method a few times on your newly created cars (don’t forget to pass a distance).
Sample output:
The 1969 Ford Mustang has travelled 123km.
The 2014 Chevy Cruze has travelled 112km.
The 2014 Chevy Cruze has travelled 155km.
The 1969 Ford Mustang has travelled 453km.
The 1969 Ford Mustang has travelled 698km.
The 2014 Chevy Cruze has travelled 598km.
Step by step
Solved in 2 steps with 2 images