Lab - Understanding JavaScript Objects
Lab - Understanding JavaScript Objects
Objectives:
In JavaScript, an object is a collection of related data and functions (methods) stored together.
Objects allow us to model real-world entities and can be used to represent anything from a
person to a city.
let objectName = {
property1: value1,
property2: value2,
method1: function() {
// method code
}
};
let ugandanFarmer = {
name: "James",
farmLocation: "Mbale",
crop: "Coffee",
numberOfAcres: 5
};
// Accessing properties
console.log(`Farmer's Name: ${ugandanFarmer.name}`);
console.log(`Farm Location: ${ugandanFarmer.farmLocation}`);
Task 1:
Create an object school that has properties like name, location, studentsCount, and
establishedYear. Log the school’s name and location to the console.
Once an object is created, you can add new properties or modify existing ones.
Task 2:
Using the school object from Task 1, add a new property schoolType (either "Primary" or
"Secondary") and change the studentsCount. Log the updated object.
let ugandanFarmer = {
name: "James",
farmLocation: "Mbale",
crop: "Coffee",
numberOfAcres: 5,
cropPrice: 3000,
calculateRevenue: function(harvestedBags) {
return harvestedBags * this.cropPrice;
}
};
Task 3:
Add a method calculateSchoolAge to the school object that calculates the age of the
school based on the current year. Use this method to calculate and print the school's age.
Objects can contain other objects. This is useful when modeling complex entities.
let kampalaCity = {
name: "Kampala",
population: 1500000,
districts: {
central: "Kampala Central",
east: "Nakawa",
north: "Kawempe",
south: "Makindye",
west: "Rubaga"
}
};
Task 4:
Create a university object that contains nested objects for departments, with each
department having a name and headOfDepartment. Log the head of one of the departments.
Part 6: Looping Through Objects
We can use the for...in loop to iterate over the properties of an object.
let ugandanFarmer = {
name: "James",
farmLocation: "Mbale",
crop: "Coffee",
numberOfAcres: 5
};
Task 5:
Using the school object from previous tasks, loop through all properties and log them to the
console.
In object methods, the this keyword refers to the object the method belongs to.
let ugandanFarmer = {
name: "James",
farmLocation: "Mbale",
crop: "Coffee",
numberOfAcres: 5,
introduceFarmer: function() {
return `Hello, my name is ${this.name} and I farm ${this.crop}
in ${this.farmLocation}.`;
}
};
console.log(ugandanFarmer.introduceFarmer());
Task 6:
Add a method schoolIntroduction to the school object that returns a string introducing
the school’s name, location, and number of students using the this keyword.
A constructor function is used to create multiple objects with similar properties and methods.
console.log(farmer1.introduce());
console.log(farmer2.introduce());
Task 7:
Create a constructor function School that takes in the name, location, studentsCount,
and schoolType. Create two new school objects and introduce them.
let ugandanFarmer = {
name: "James",
farmLocation: "Mbale",
crop: "Coffee"
};
Task 8:
Using the school object, destructure the name, location, and studentsCount into
variables and log them.
Let’s create a more complex example of a Ugandan business selling different products.
let business = {
name: "Kampala Electronics",
location: "Kampala",
products: [
{ name: "TV", price: 1000000, quantity: 50 },
{ name: "Radio", price: 150000, quantity: 200 },
{ name: "Laptop", price: 3000000, quantity: 30 }
],
calculateTotalValue: function() {
let total = 0;
for (let i = 0; i < this.products.length; i++) {
total += this.products[i].price *
this.products[i].quantity;
}
return total;
}
};
Task 9:
Create an object for a marketVendor who sells multiple products (e.g., bananas, cassava,
tomatoes). Each product should have a price and quantity. Add a method to calculate the
total revenue for all products in stock.
Lab Summary:
● Explored how to create, access, and modify objects and their properties.
● Learned how to add methods to objects and use the this keyword.
● Created nested objects and used loops to iterate through object properties.
● Explored constructor functions to create multiple objects and destructured objects.
● Applied object concepts to real-world scenarios, including farming, schools, and
businesses.
Additional Exercises:
This lab will provide a comprehensive understanding of how to work with JavaScript objects
while using relatable Ugandan