Association in JavaScript is an object-oriented programming concept that defines a relationship between two or more independent objects. The associated objects can interact with each other while maintaining their own separate existence.
- It represents a general relationship between independent objects.
- Associated objects can communicate without depending on each other's lifecycle.
- Association can be unidirectional or bidirectional based on how references are maintained.
Approach 1: Unidirectional Association Using Object References
In this approach, one object maintains a reference to another object, allowing it to access or interact with the associated object. The relationship works in one direction because only one object stores the reference.
- Create separate objects representing different entities.
- Store a reference to one object inside another object.
- Use the reference to access properties or methods of the associated object.
- The associated objects continue to exist independently.
Example: The following example demonstrates a unidirectional association between a student and a teacher.
const teacher = {
name: "Alen",
subject: "JavaScript"
};
const student = {
name: "John",
age: 20,
teacher: teacher,
displayDetails() {
console.log(`Student: ${this.name}`);
console.log(`Age: ${this.age}`);
console.log(`Teacher: ${this.teacher.name}`);
console.log(`Subject: ${this.teacher.subject}`);
}
};
// Display student details
student.displayDetails();
Output
Student: John Age: 20 Teacher: Alen Subject: JavaScript
Here, the student object stores a reference to the teacher object. The student can access teacher information, but the teacher does not maintain a reference to the student, making this a unidirectional association.
Approach 2: Bidirectional Association Using Classes
In this approach, both related objects maintain references to each other. This allows either object to access information about the other, creating a relationship in both directions.
- Create separate classes for the related entities.
- Add properties to store references between the objects.
- Use methods to establish and maintain the relationship.
- Allow both objects to access their associated objects.
Example: The following example demonstrates a bidirectional association between employees and a department.
class Employee {
constructor(name) {
this.name = name;
this.department = null;
}
assignDepartment(department) {
this.department = department;
}
displayDetails() {
console.log(`Employee: ${this.name}`);
if (this.department) {
console.log(
`Department: ${this.department.name}`
);
}
}
}
class Department {
constructor(name) {
this.name = name;
this.employees = [];
}
addEmployee(employee) {
this.employees.push(employee);
employee.assignDepartment(this);
}
displayEmployees() {
console.log(`Department: ${this.name}`);
console.log("Employees:");
this.employees.forEach((employee, index) => {
console.log(`${index + 1}. ${employee.name}`);
});
}
}
// Create independent employee objects
const employee1 = new Employee("John");
const employee2 = new Employee("Emma");
const department = new Department("Development");
// Create associations
department.addEmployee(employee1);
department.addEmployee(employee2);
// Display department details
department.displayEmployees();
console.log("\nEmployee Details:");
employee1.displayDetails();
Output
Department: Development Employees: 1. John 2. Emma Employee Details: Employee: John Department: Development
In this example, the Department object maintains references to its employees, while each Employee object maintains a reference to its department. This creates a bidirectional association, allowing both sides to interact while remaining independent objects.