This repository contains solutions for 3 tasks focused on
Object-Oriented Programming (OOP) concepts in JavaScript.
The homework covers this context, classes, private properties,
class methods, and encapsulation.
Each task is implemented in a separate file:
task-1.js, task-2.js, task-3.js.
After completing Module 6, you are almost at the core of JavaScript 💪 This homework reinforces your understanding of:
- The
thiskeyword in different contexts thisin global scope, object methods, arrow functions, and callbacks- The
call,apply, andbindmethods - Object-Oriented Programming fundamentals
- Classes, instances, and public interfaces
- Prototypes and prototypal inheritance
- Creating objects of the same type with shared behavior and different data
The final step is to correctly use this, build classes for managing data,
and work with string constructors.
Refactor an existing object to correctly use the this keyword.
The task simulates a real-world scenario where a developer left broken code
for managing user accounts in a food delivery service.
You must fix object methods so they properly access and update object properties
using this.
const customer = {
username: "Mango",
balance: 24000,
discount: 0.1,
orders: ["Burger", "Pizza", "Salad"],
};
✅ What was practiced:
- Correct usage of
thisinside object methods - Updating object state via methods
- Working with arrays and numeric calculations inside an object
✅ Expected behavior:
- Getting and updating balance and discount
- Adding new orders
- Returning updated orders list
Create a Storage class to manage a warehouse inventory. The class stores items in a private property and provides a clean public interface for interaction.
Defined methods:
getItems()— returns the current list of itemsaddItem(newItem)— adds a new item to storageremoveItem(itemToRemove)— removes an item by name
const storage = new Storage(["Nanitoids", "Prolonger", "Antigravitator"]);
console.log(storage.getItems());
storage.addItem("Droid");
storage.removeItem("Prolonger");
✅ What was practiced:
- Creating classes with constructors
- Using private class fields
- Encapsulation and controlled access to data
- Modifying internal state through class methods
Create a StringBuilder class for flexible string manipulation. The class stores a string in a private property and provides methods to modify it from different sides.
Defined methods:
getValue()— returns the current string valuepadStart(str)— adds a string to the beginningpadEnd(str)— adds a string to the endpadBoth(str)— adds a string to both beginning and end
const builder = new StringBuilder(".");
builder.padStart("^");
builder.padEnd("^");
builder.padBoth("=");
✅ What was practiced:
- Working with private class fields
- String manipulation inside class methods
- Designing a clear and minimal public API
With this homework, you practiced core Object-Oriented Programming concepts in JavaScript:
- Proper usage of
this - Building reusable classes
- Encapsulation with private properties
- Clean and predictable class interfaces
- Real-world scenarios using objects and classes
These tasks strengthen your understanding of OOP and prepare you for building scalable, maintainable JavaScript applications 🚀