$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

CMS Website Development Checklist: A Step-by-Step Guide for Business Owners

Introduction Your website is the digital headquarters of your business. It operates as your 24/7 sales representative, your primary marketing asset, and the first point of contact…

Read More

How to Choose the Right CMS for Your Business Website

Introduction A website serves as the digital engine of a modern business, but the software powering it determines how fast, secure, and adaptable that engine truly is….

Read More

Common CMS Website Mistakes and How to Avoid Them

Introduction A Content Management System (CMS) offers an accessible way to build, manage, and update a website without needing to write code from scratch. Because platforms like…

Read More

The Amaravati Discovery Guide: Sightseeing and Local Events

Introduction Amaravati is a destination where thousands of years of history, profound spiritual heritage, and evolving modern regional dynamics intersect. Situated along the southern banks of the…

Read More

Navigating DataOps Courses, Certifications, and Professional Services

Introduction Modern organizations rely on fast, accurate, and accessible data to drive operations, customer experiences, and strategic decisions. Yet, as data volumes grow and data architectures expand…

Read More

Integrating AI Software Development with Cloud and DevOps Pipelines

Introduction Modern software engineering has moved far beyond traditional application coding and routine deployments. Organizations across industries are racing to build intelligent, cloud-native platforms capable of processing…

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