In this lab, you’ll explore JavaScript classes and learn how they can be used to represent real-world entities and their behaviors. Classes are templates that allow you to create objects with pre-defined properties and methods, making it easier to structure and organize code, especially in object-oriented programming.
This lab introduces two custom JavaScript classes:
- Person: Represents a person with properties like
name
andage
, as well as methods to greet and update the person's age. - Vehicle: Models a vehicle with attributes such as
make
,model
,year
, and astart
orstop
state.
By the end of this lab, you will:
- Understand how to define classes and constructors in JavaScript.
- Be able to create instances of classes and call their methods to see different behaviors.
- Learn how to modify and retrieve an object's properties.
This foundational lab will help you build a strong base for creating more complex, interactive applications in JavaScript that rely on organized, reusable code structures.
Your project should be structured as follows:
javascript-classes/
├── index.js
└── README.md
-
Create a project folder named
javascript-classes
to store your project files. -
Inside the
javascript-classes
folder, create a file namedindex.js
. This will be your main JavaScript file where all your code will be written.
In this step, you will create a Person
class to model a person with specific attributes and behaviors. The Person
class will have properties to store a person’s name
and age
, and methods to enable each Person
instance to introduce itself and celebrate its birthday. Later, you’ll call this class to create and log different Person
objects with unique names and ages, demonstrating how the class can be reused to represent various individuals.
- Add the following code to your
index.js
file:
// Person class definition
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
haveBirthday() {
this.age += 1;
console.log(`It's my birthday! I am now ${this.age} years old.`);
}
}
Explanation:
-
Creating the Class Structure: The
Person
class serves as a template for creating objects that share common properties (name
andage
) and methods (greet
andhaveBirthday
). This is an example of object-oriented programming (OOP) in JavaScript, where classes help define and organize code based on real-world objects. -
Constructor Function: The
constructor(name, age)
is a special function that runs automatically when a newPerson
instance is created. It sets up thename
andage
properties based on provided arguments, initializing eachPerson
object with specific values. -
greet
Method: Thegreet
method logs a personalized greeting that includes thename
property. By using template literals (${}
syntax), the greeting can dynamically include the person’s name, making eachPerson
instance able to introduce itself. -
haveBirthday
Method: ThehaveBirthday
method increases theage
by one each time it’s called, simulating a birthday. It also logs a celebratory message with the updated age, showcasing how classes can change an object’s properties over time. -
Calling the Class Later: After defining the class, we’ll use it to create different
Person
instances and log various values to the console. Each instance will store a uniquename
andage
, showing how this class can represent multiple people with distinct attributes and behaviors.
In this step, you’ll create a Vehicle
class that models a vehicle with properties for make
, model
, year
, and a state to track if the vehicle is running. The Vehicle
class will include methods to start and stop the vehicle and calculate its age. Later, you’ll use this class to create and log multiple Vehicle
objects with unique details.
- Add the following code to your
index.js
file:
// Vehicle class definition
class Vehicle {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.isRunning = false;
}
start() {
this.isRunning = true;
console.log(`${this.make} ${this.model} has started.`);
}
stop() {
this.isRunning = false;
console.log(`${this.make} ${this.model} has stopped.`);
}
getAge(currentYear) {
return currentYear - this.year;
}
}
Explanation:
-
Creating the Class Structure: The
Vehicle
class provides a blueprint for creating vehicle objects with standard properties (make
,model
,year
, andisRunning
) and methods (start
,stop
, andgetAge
). This structure helps model a vehicle’s basic features and behaviors. -
Constructor Function: The
constructor(make, model, year)
function initializes each newVehicle
instance with specific values formake
,model
, andyear
. It also setsisRunning
tofalse
by default, indicating that the vehicle is initially off. -
start
Method: Thestart
method sets theisRunning
property totrue
and logs a message to indicate that the vehicle has started. This simulates turning the vehicle on. -
stop
Method: Thestop
method setsisRunning
tofalse
and logs a message indicating the vehicle has stopped. This simulates turning the vehicle off. -
getAge
Method: ThegetAge
method calculates the vehicle’s age by subtracting itsyear
from thecurrentYear
provided as an argument. This allows us to determine how old the vehicle is based on the current date. -
Calling the Class Later: After defining this class, we’ll use it to create different
Vehicle
instances and log their properties and states to the console. Each instance will have unique values formake
,model
, andyear
, showcasing how theVehicle
class can represent different vehicles with distinct characteristics.
In this step, you’ll test the Person
class by creating an instance of a person and calling its methods to observe the output. This allows you to verify that the class functions as expected and that each method behaves as intended.
- Add the following test code to your
index.js
file:
// Testing the Person class
const alice = new Person("Alice", 30); // Creates a new Person instance named 'alice' with 'name' set to "Alice" and 'age' set to 30
alice.greet(); // Calls the greet method on 'alice', logging a greeting with her name
alice.haveBirthday(); // Calls the haveBirthday method on 'alice', increasing her age by 1 and logging a birthday message
- Run your code using Node.js in the terminal:
node index.js
Hello, my name is Alice and I am 30 years old.
It's my birthday! I am now 31 years old.
Explanation:
-
Creating the
alice
Instance:const alice = new Person("Alice", 30);
creates a new instance of thePerson
class namedalice
withname
set to"Alice"
andage
set to30
. This line uses theconstructor
method of thePerson
class to initializealice
's properties with these initial values. -
Calling
greet
Method:alice.greet();
calls thegreet
method on thealice
instance, which logs the message"Hello, my name is Alice and I am 30 years old."
to the console. This shows that thegreet
method can access bothalice
'sname
andage
properties and incorporate them dynamically into the greeting message. -
Calling
haveBirthday
Method:alice.haveBirthday();
calls thehaveBirthday
method onalice
, which increasesalice
’sage
by1
, changing it from30
to31
. It then logs the message"It's my birthday! I am now 31 years old."
to confirm thathaveBirthday
successfully modifies and accesses theage
property.
Now, let’s create another Person
instance with different values to further test the Person
class. This demonstrates the flexibility of the class, allowing each instance to hold unique data and call methods independently.
- Add the following code to your
index.js
file:
// Creating another instance of the Person class
const bob = new Person("Bob", 25); // Creates a new Person instance named 'bob' with 'name' set to "Bob" and 'age' set to 25
bob.greet(); // Calls the greet method on 'bob', logging a greeting with his name and age
bob.haveBirthday(); // Calls the haveBirthday method on 'bob', increasing his age by 1 and logging a birthday message
- Run your code using Node.js in the terminal to test both alice and bob instances:
node index.js
Hello, my name is Bob and I am 25 years old.
It's my birthday! I am now 26 years old.
Explanation:
-
Creating the
bob
Instance:const bob = new Person("Bob", 25);
creates a newPerson
instance namedbob
withname
set to"Bob"
andage
set to25
. Similar toalice
,bob
is initialized with specific values using theconstructor
method. -
Calling
greet
Method onbob
:bob.greet();
calls thegreet
method onbob
, logging"Hello, my name is Bob and I am 25 years old."
to the console. This confirms that thegreet
method accesses and dynamically incorporatesbob
's uniquename
andage
values. -
Calling
haveBirthday
Method onbob
:bob.haveBirthday();
calls thehaveBirthday
method onbob
, which incrementsbob
'sage
by 1 (from 25 to 26) and logs"It's my birthday! I am now 26 years old."
. This demonstrates thathaveBirthday
can successfully modify and access theage
property, showing the independent behavior of eachPerson
instance.
Now, let’s create an instance of the Vehicle
class and test its methods to ensure that they work as expected. This demonstrates how the class manages and operates on its own state independently.
-
Add the following code to your
index.js
file:// Testing the Vehicle class const car = new Vehicle("Toyota", "Corolla", 2015); // Creates a new Vehicle instance named 'car' with 'make' set to "Toyota", 'model' set to "Corolla", and 'year' set to 2015 car.start(); // Calls the start method on 'car', logging "Toyota Corolla has started." console.log(`Is the car running? ${car.isRunning}`); // Logs the current running status of 'car', expected to be true car.stop(); // Calls the stop method on 'car', logging "Toyota Corolla has stopped."
-
Run your code using Node.js in the terminal to test the
Vehicle
instance:node index.js
Toyota Corolla has started.
Is the car running? true
Toyota Corolla has stopped.
Explanation:
-
Creating the
car
Instance:const car = new Vehicle("Toyota", "Corolla", 2015);
creates a newVehicle
instance namedcar
with specific values formake
,model
, andyear
using theconstructor
method. -
Calling
start
Method oncar
:car.start();
calls thestart
method oncar
, logging"Toyota Corolla has started."
to the console. This confirms that thestart
method correctly changes theisRunning
property totrue
and logs the starting message. -
Checking
isRunning
Property oncar
:console.log(\
Is the car running? ${car.isRunning}`);accesses the
isRunningproperty on
car, which is expected to be
true`, indicating that the car is currently running. -
Calling
stop
Method oncar
:car.stop();
calls thestop
method oncar
, which setsisRunning
tofalse
and logs"Toyota Corolla has stopped."
This demonstrates thestop
method’s ability to change and access theisRunning
property independently.
Now, let’s create another Vehicle
instance to further test the functionality of the Vehicle
class. This demonstrates that each instance maintains its own state and can operate independently.
-
Add the following code to your
index.js
file:// Creating and testing another instance of the Vehicle class const truck = new Vehicle("Ford", "F-150", 2018); // Creates a new Vehicle instance named 'truck' with 'make' set to "Ford", 'model' set to "F-150", and 'year' set to 2018 truck.start(); // Calls the start method on 'truck', logging "Ford F-150 has started." console.log(`Is the truck running? ${truck.isRunning}`); // Logs the current running status of 'truck', expected to be true truck.stop(); // Calls the stop method on 'truck', logging "Ford F-150 has stopped."
-
Run your code using Node.js in the terminal to test the
truck
instance:node index.js
Ford F-150 has started.
Is the truck running? true
Ford F-150 has stopped.
Explanation:
-
Creating the
truck
Instance:const truck = new Vehicle("Ford", "F-150", 2018);
creates a newVehicle
instance namedtruck
with specified values formake
,model
, andyear
using theconstructor
method. -
Calling
start
Method ontruck
:truck.start();
calls thestart
method ontruck
, logging"Ford F-150 has started."
to the console. This confirms that thestart
method correctly changes theisRunning
property totrue
and logs the starting message. -
Checking
isRunning
Property ontruck
:console.log(\
Is the truck running? ${truck.isRunning}`);accesses the
isRunningproperty on
truck, expected to be
true`, indicating that the truck is currently running. -
Calling
stop
Method ontruck
:truck.stop();
calls thestop
method ontruck
, settingisRunning
tofalse
and logging"Ford F-150 has stopped."
This demonstrates that eachVehicle
instance, liketruck
, can operate independently and modify its own state.
In this step, we’ll calculate the age of each Vehicle
instance using the current year. This helps illustrate how instance methods can use parameters to perform calculations based on both instance properties and external values.
-
Add the following code to your
index.js
file:// Calculating the age of each vehicle instance const currentYear = new Date().getFullYear(); // Gets the current year from the Date object console.log(`The car is ${car.getAge(currentYear)} years old.`); // Logs the age of 'car', based on the current year console.log(`The truck is ${truck.getAge(currentYear)} years old.`); // Logs the age of 'truck', based on the current year
-
Run your code using Node.js in the terminal to see the age of each vehicle:
node index.js
(Output will vary depending on the current year)
The car is X years old.
The truck is Y years old.
Explanation:
-
Getting the Current Year:
const currentYear = new Date().getFullYear();
retrieves the current year by creating a new instance of JavaScript's built-inDate
object and calling itsgetFullYear()
method. TheDate
object represents a single moment in time, andgetFullYear()
extracts only the four-digit year from it. This value is stored incurrentYear
and is then passed as an argument to each vehicle’sgetAge
method. By using the actual current year, we enable thegetAge
method to dynamically calculate the vehicle's age at any point in time, keeping the code future-proof and adaptable. -
Calculating
car
Age:console.log(\
The car is ${car.getAge(currentYear)} years old.`);calls the
getAgemethod on the
carinstance, passing in
currentYearas a parameter. Inside
getAge, the method subtracts the
yearproperty of
car(which is
2015) from
currentYear. This operation gives the number of years that have passed since the
car` was manufactured, thereby determining its age. -
Calculating
truck
Age:console.log(\
The truck is ${truck.getAge(currentYear)} years old.`);similarly calls the
getAgemethod on the
truckinstance, also passing
currentYearas a parameter. Since
truckwas created with the
yearproperty set to
2018, the
getAgemethod subtracts
2018from
currentYear. This allows the
truck` instance to display its unique age based on its manufacturing year.
In this way, the getAge
method leverages the currentYear
parameter to calculate and return the age of each individual vehicle instance, making it flexible and responsive to real-time values.
In this final step, you’ll commit your changes and push your project to GitHub to save and share your work. This will ensure that your project is versioned and backed up remotely.
-
Initialize Git (if not already initialized):
git init
-
Add All Changes to Staging:
git add .
-
Commit Your Changes:
git commit -m "Add JavaScript classes for Person and Vehicle with methods"
-
Connect to Your GitHub Repository (if not already connected):
- Replace
<username>
with your GitHub username and<repository-name>
with the name of your repository.
git remote add origin https://github.com/<username>/<repository-name>.git
- Replace
-
Push to GitHub:
git push -u origin main
In this step, we committed our changes and pushed the project to GitHub to create a backup and enable version control:
- Initialized Git: We ran
git init
to start version control in the project folder if it wasn’t already initialized. - Added Changes to Staging: The
git add .
command staged all project files for the commit. - Committed the Changes: We used
git commit
with a descriptive message to save our work. - Connected to GitHub: We linked our local repository to a GitHub repository using
git remote add origin
. - Pushed to GitHub: Finally,
git push -u origin main
uploaded our code to themain
branch on GitHub.
This step ensures that your project is versioned and safely stored in GitHub, enabling easy sharing and tracking of changes.
In this lab, you learned how to use JavaScript classes to create structured, reusable code representing objects with specific properties and methods. By following each step, you gained practical experience with:
- Defining classes and constructors to initialize objects with predefined values.
- Implementing and calling class methods to modify and retrieve object properties.
- Creating multiple instances of classes like
Person
andVehicle
, each maintaining independent properties and states. - Utilizing methods like
getAge()
to calculate values dynamically, demonstrating the interaction between instance properties and external parameters.
- Object-Oriented Design: Classes are powerful templates in JavaScript, allowing you to organize code around real-world concepts and shared behaviors.
- Method Calls and State Management: Methods such as
start
,stop
, andhaveBirthday
show how classes can manage and modify internal states of objects. - Flexible Calculations with Parameters: Passing arguments like
currentYear
into methods illustrates how class methods can perform calculations based on both instance and external values, making the code responsive to real-time data.
With these skills, you’re now equipped to use JavaScript classes to build organized, dynamic applications that rely on structured, reusable code. Continue practicing by creating more complex classes and exploring how object-oriented programming enhances code readability, reusability, and scalability!
🛑 Only use this as a reference 🛑
💾 Not something to copy and paste 💾
Note: This lab references a solution file located here (link not shown).
© All rights reserved to ThriveDX