$100 Website Offer

Get your personal website + domain for just $100.

Limited Time Offer!

Claim Your Website Now

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.

Related Posts

The Comprehensive Blueprint for Free Vehicle Rental Management Software

Introduction The growth of the global vehicle rental industry has been fueled by a shift in consumer behavior toward the sharing economy. As travel demands rise and…

Read More

Comparing the World’s Best Heart Surgery Hospitals and Clinical Teams

Introduction The Growing Burden of Cardiovascular Diseases Cardiovascular diseases (CVDs) remain the leading cause of mortality globally, placing an unprecedented burden on healthcare delivery systems. Ischemic heart…

Read More

Affordable Website Design for Growing Companies: The Complete Strategic Guide

Introduction In the digital landscape, a website serves as a company’s modern storefront, primary lead generator, and foundational brand asset. For growing companies, startups, and small businesses,…

Read More

The Ultimate Guide to a User-Friendly and Mobile-Ready Business Website

Introduction A corporate website is no longer just a passive digital brochure; it functions as your primary storefront, lead generator, and brand ambassador. Over 60% of all…

Read More

Best Places to Visit in Goa: The Ultimate Travel Guide to Beaches, Nightlife & Itineraries

Introduction Finding the best places to visit in Goa means opening the door to a paradise that seamlessly blends laid-back coastal living with high-octane excitement. Whether you…

Read More

Best Countries for Plastic Surgery: Top Global Cosmetic Hospitals, Surgeons, and Costs

Introduction Deciding to undergo an aesthetic procedure is a deeply personal journey, and finding the best cosmetic hospitals in the world is the critical first step toward…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x