Object Oriented JavaScript

Object-oriented programming (OOP) in JavaScript involves creating objects and defining their behavior through methods and properties. JavaScript supports OOP concepts such as encapsulation, inheritance, and polymorphism, although its implementation differs from classical OOP languages like Java or C++. Here’s an overview of OOP in JavaScript:

1. Objects and Prototypes:

JavaScript is a prototype-based language, which means objects inherit properties and methods from other objects. Objects in JavaScript can be created using object literals {}, constructor functions, or classes (introduced in ES6).

Object Literals:

let person = {
  firstName: 'John',
  lastName: 'Doe',
  fullName: function() {
    return this.firstName + ' ' + this.lastName;
  }
};

Constructor Functions:

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.fullName = function() {
    return this.firstName + ' ' + this.lastName;
  };
}

let person = new Person('John', 'Doe');

Classes (ES6):

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  fullName() {
    return this.firstName + ' ' + this.lastName;
  }
}

let person = new Person('John', 'Doe');

2. Encapsulation:

Encapsulation is the bundling of data and methods that operate on the data within a single unit, typically a class or object. In JavaScript, encapsulation can be achieved using closures or private class fields (introduced in ES2022).

Using Closures:

function Counter() {
  let count = 0;

  this.increment = function() {
    count++;
  };

  this.getCount = function() {
    return count;
  };
}

let counter = new Counter();
counter.increment();
console.log(counter.getCount()); // Output: 1

Private Class Fields (ES2022):

class Counter {
  #count = 0;

  increment() {
    this.#count++;
  }

  getCount() {
    return this.#count;
  }
}

let counter = new Counter();
counter.increment();
console.log(counter.getCount()); // Output: 1

3. Inheritance:

Inheritance is the mechanism by which one class (subclass) inherits properties and methods from another class (superclass). In JavaScript, inheritance is implemented using prototypes.

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  console.log(this.name + ' makes a noise');
};

function Dog(name) {
  Animal.call(this, name);
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.speak = function() {
  console.log(this.name + ' barks');
};

let dog = new Dog('Buddy');
dog.speak(); // Output: Buddy barks

4. Polymorphism:

Polymorphism allows objects to be treated as instances of their parent class. In JavaScript, polymorphism is achieved through method overriding.

class Animal {
  speak() {
    console.log('Animal makes a noise');
  }
}

class Dog extends Animal {
  speak() {
    console.log('Dog barks');
  }
}

let animal = new Animal();
let dog = new Dog();
animal.speak(); // Output: Animal makes a noise
dog.speak(); // Output: Dog barks

Object-oriented programming in JavaScript provides a flexible and powerful way to structure and organize code, enabling developers to build complex applications with ease. Understanding OOP principles and their implementation in JavaScript is essential for writing maintainable and scalable code.