Object-Oriented Programming (OOP) in JavaScript is a way of writing and organizing code using objects. Objects are containers for data (called properties) and functions (called methods). OOP makes your code easier to understand, reuse, and maintain.
Here’s a simple explanation of the main OOP concepts in JavaScript:
1. Objects
An object is like a real-world thing (e.g., a car or a person). It has properties (describing the object) and methods (actions the object can perform).
Example:
const car = {
brand: "Toyota", // property
model: "Corolla", // property
start: function() { // method
console.log("The car has started.");
}
};
// Accessing properties and methods
console.log(car.brand); // Output: Toyota
car.start(); // Output: The car has started.
2. Classes
A class is a blueprint for creating objects with the same structure. It defines properties and methods that the objects will have.
Example:
class Person {
constructor(name, age) {
this.name = name; // property
this.age = age; // property
}
greet() { // method
console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);
}
}
// Create objects from the class
const person1 = new Person("Alice", 25);
const person2 = new Person("Bob", 30);
person1.greet(); // Output: Hi, my name is Alice and I am 25 years old.
person2.greet(); // Output: Hi, my name is Bob and I am 30 years old.
3. Inheritance
Inheritance allows one class to inherit properties and methods from another class. This avoids code duplication.
Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
// Dog class inherits from Animal
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog("Buddy");
dog.speak(); // Output: Buddy barks.
4. Encapsulation
Encapsulation is the concept of keeping data safe by restricting direct access to it. In JavaScript, you can use #
for private properties or methods (only accessible inside the class).
Example:
class BankAccount {
#balance; // private property
constructor(initialBalance) {
this.#balance = initialBalance;
}
deposit(amount) {
this.#balance += amount;
console.log(`Deposited: ${amount}`);
}
getBalance() {
return this.#balance; // controlled access
}
}
const account = new BankAccount(100);
account.deposit(50); // Output: Deposited: 50
console.log(account.getBalance()); // Output: 150
5. Polymorphism
Polymorphism allows different classes to have methods with the same name but different implementations.
Example:
class Shape {
area() {
console.log("Calculating area...");
}
}
class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}
area() {
return Math.PI * this.radius ** 2; // Circle-specific implementation
}
}
class Square extends Shape {
constructor(side) {
super();
this.side = side;
}
area() {
return this.side ** 2; // Square-specific implementation
}
}
const circle = new Circle(5);
console.log(circle.area()); // Output: 78.54
const square = new Square(4);
console.log(square.area()); // Output: 16
Benefits of OOP in JavaScript
- Code Reusability: Classes and inheritance allow you to reuse code efficiently.
- Modularity: Code is divided into objects and classes, making it easier to manage.
- Easier Maintenance: Changes in one part of the code do not affect other parts.
- Scalability: OOP helps in building large-scale applications.
- Clarity: Code becomes more structured and easier to read.