Course Content
Advanced JavaScript Mastery
Advanced JavaScript Mastery
Challenge: Building a Class
Task
You're creating a system to manage a car rental fleet. Each vehicle has specific details: the make, model, and year. Build a Vehicle
class to represent these details for each car.
- Create the Class: Define a class named
Vehicle
; - Add a Constructor: Inside the
Vehicle
class:- Define a constructor that takes three parameters:
make
,model
, andyear
; - Assign these parameters to the class properties.
- Define a constructor that takes three parameters:
- Create and Test Instances:
- Create a
Vehicle
instance namedcar1
with the values"Toyota"
,"Camry"
, and2020
; - Create another instance named
car2
with"Ford"
,"Mustang"
, and2018
; - Log the properties for each car instance.
- Create a
class _____ { constructor(_____, _____, _____) { this._____ = _____; this._____ = _____; this._____ = _____; } } // Create instances const car1 = new _____(_____, _____, _____); const car2 = new _____(_____, _____, _____); // Output the details console.log(car1._____); // Expected: Toyota console.log(car1._____); // Expected: Camry console.log(car1._____); // Expected: 2020 console.log(car2._____); // Expected: Ford console.log(car2._____); // Expected: Mustang console.log(car2._____); // Expected: 2018
- Define a class named
Vehicle
; - Add a constructor with three parameters:
make
,model
, andyear
; - Inside the constructor, assign each parameter to a property using
this
; - Create an instance of
Vehicle
namedcar1
with values"Toyota"
,"Camry"
, and2020
; - Create another instance of
Vehicle
namedcar2
with values"Ford"
,"Mustang"
, and2018
; - Use
console.log()
to display the properties ofcar1
andcar2
.
class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } } // Create instances const car1 = new Vehicle('Toyota', 'Camry', 2020); const car2 = new Vehicle('Ford', 'Mustang', 2018); // Output the details console.log(car1.make); // Output: Toyota console.log(car1.model); // Output: Camry console.log(car1.year); // Output: 2020 console.log(car2.make); // Output: Ford console.log(car2.model); // Output: Mustang console.log(car2.year); // Output: 2018
Thanks for your feedback!