Aggregation in JavaScript
Aggregation is a fundamental concept in object-oriented programming that facilitates the creation of complex objects by combining simpler ones. It establishes a “has-a” relationship between objects, where one object contains references to other objects as its components. This relationship enhances code reusability and modularity in JavaScript applications.
Table of Content
- Aggregation using Object Properties
- Aggregation using Arrays
Approach 1: Aggregation using Object Properties
- Create an object that contains properties representing the components you want to aggregate.
- These properties can be references to other objects, functions, or any data type.
- Establish an aggregation relationship by including one object within the other as a property.
- Allow one object to reference and use another, providing a way to model aggregation.
Example: In this example, we showcase aggregation in JavaScript with two book objects, book1
and book2
, aggregated within a library object, GFG
. The library object has methods to add books dynamically and display their information. A third book, book3
, is added to the library, and the display method is used to show details of all the books in the library.
Javascript
const book1 = { title: "The Great Gatsby" , author: "F.Scott Fitzgerald" }; const book2 = { title: "The w3wiki" , author: "Geek" }; // Create an object representing a library that // aggregates books const GFG = { name: "My Library" , books: [book1, book2], addBook: function (book) { this .books.push(book); }, displayBooks: function () { console.log(`Books in ${ this .name}:`); this .books.forEach((book, index) => { console.log( `${index + 1}. Title: ${book.title}, Author: ${book.author}`); }); } }; // Add more books const book3 = { title: "2023" , author: "George Orwell" }; GFG.addBook(book3); // Display the books GFG.displayBooks(); |
Books in My Library: 1. Title: The Great Gatsby, Author: F.Scott Fitzgerald 2. Title: The w3wiki, Author: Geek 3. Title: 2023, Author: George Orwell
Approach 2 : Aggregation using Arrays
- Create an object that contains an array property to hold aggregated items.
- Perform operations like adding, removing, or manipulating items within the array.
- Aggregation starts with collecting data from various sources, such as user input, databases, or external files.
- Typically, store the collected data in an array data structure capable of holding multiple values of the same type.
- Arrays provide a way to represent a collection of related data items.
- Each item in the array is stored at a specific index, allows easy access and manipulation of individual elements.
- Arrays are often used to group and classify data, allowing effective organization, such as storing scores of students in a class.
Example: In this example, player objects are created using a constructor function, and a team object is formed to aggregate these players. The team object includes methods to add, remove, and display players. Additional players are added, the team is displayed, a player is removed, and the updated team is displayed.
Javascript
function Players(name, age) { this .name = name; this .age = age; } // Create Player objects const player1 = new Players( "Kumar" , 31); const player2 = new Players( "Bob" , 29); const team = { name: "Red Team" , players: [player1, player2], addPlayer: function (player) { this .players.push(player); }, removePlayer: function (playerName) { const indexToRemove = this .players.findIndex(player => player.name === playerName); if (indexToRemove !== -1) { this .players.splice(indexToRemove, 1); } }, displayTeam: function () { console.log(`Team: ${ this .name}`); console.log( "Players:" ); this .players.forEach((player, index) => { console.log( `${index + 1}. Name: ${player.name}, Age: ${player.age}`); }); } }; // Add more players to the team const player3 = new Players( "Raj" , 32); team.addPlayer(player3); // Display the team and its players team.displayTeam(); team.removePlayer( "Bob" ); // Display the updated team team.displayTeam(); |
Team: Red Team Players: 1. Name: Kumar, Age: 31 2. Name: Bob, Age: 29 3. Name: Raj, Age: 32 Team: Red Team Players: 1. Name: Kumar, Age: 31 2. Name: Raj, Age: 32
Contact Us