$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

Exploring Dining Options Made Simple: How to Discover Restaurants and Connect with Venues

Introduction Deciding where to eat can sometimes feel surprisingly difficult. Whether you are looking for a quick lunch during a busy workday, planning a celebratory family dinner,…

Read More

Elevate Your Daily Research and Industry Insights With a Smart AI News Aggregator

Introduction Following the news today can feel like standing in the middle of a busy railway station while dozens of announcers shout different updates at the exact…

Read More

Website Contact Page Design: Best Practices for Usability, Trust, and Conversions

Introduction A website contact page is often the final destination before a prospect decides whether to do business with you. Potential buyers navigate through your service pages,…

Read More

Smart Travel Connectivity: How to Evaluate Data, Networks, and Roaming Plans

Introduction Stepping off an international flight without internet access creates immediate hurdles. Modern travelers depend on connected phones to hail airport rides, check digital boarding passes, navigate…

Read More

Mautic vs listmonk vs Dittofeed — Complete Self-Hosted Marketing Automation Comparison

For your requirements—automatic lead capture, centralized contacts, segmentation, Amazon SES, WhatsApp bulk campaigns, automated follow-ups, low operating costs, and complete data ownership—Mautic is my first recommendation. However,…

Read More

Global Mobile Network Identifiers: A Practical Guide to Telecom Codes and Provider Records

Introduction Every time a mobile phone connects to a cellular tower, a quiet digital handshake takes place. Whether a smartphone is roaming in a foreign airport, an…

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