Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Create a JavaScript Class | Mastering JavaScript Classes and Inheritance
Advanced JavaScript Mastery

book
Challenge: Create a JavaScript 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.

  1. Create the Class: Define a class named Vehicle;

  2. Add a Constructor: Inside the Vehicle class:

    • Define a constructor that takes three parameters: make, model, and year;

    • Assign these parameters to the class properties.

  3. Create and Test Instances:

    • Create a Vehicle instance named car1 with the values "Toyota", "Camry", and 2020;

    • Create another instance named car2 with "Ford", "Mustang", and 2018;

    • Log the properties for each car instance.

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
1234567891011121314151617181920
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
copy
  • Define a class named Vehicle;

  • Add a constructor with three parameters: make, model, and year;

  • Inside the constructor, assign each parameter to a property using this;

  • Create an instance of Vehicle named car1 with values "Toyota", "Camry", and 2020;

  • Create another instance of Vehicle named car2 with values "Ford", "Mustang", and 2018;

  • Use console.log() to display the properties of car1 and car2.

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
1234567891011121314151617181920
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
copy

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt