OOP in Javascript

Object-Oriented Programming (OOPs) is a programming paradigm that uses objects and classes to structure code. JavaScript is a multi-paradigm language, and while it supports various programming styles, it also allows developers to implement object-oriented principles. Here are key concepts related to OOPs in JavaScript:

1. Objects:
In JavaScript, an object is a collection of key-value pairs, where each key is a string (or symbol) and each value can be of any data type, including other objects. Objects in JavaScript can represent real-world entities and encapsulate data and behavior.

let car = {
brand: "Toyota",
model: "Camry",
year: 2022,
start: function() {
console.log("Engine started.");
}
};
car.start(); // Calling a method on the object

2. Classes:
JavaScript introduced the class syntax in ECMAScript 2015 (ES6) to support a more classical style of object-oriented programming. Classes provide a blueprint for creating objects with shared properties and methods.

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log("Hello, I'm " + this.name);
}
}
let person1 = new Person("John", 30);
person1.sayHello();

3. Constructor:
The constructor method is a special method in a class that is called when an object is created using the new keyword. It initializes the object’s properties.

class Dog {
constructor(name, breed) {
this.name = name;
this.breed = breed;
}
bark() {
console.log("Woof! Woof!");
}
}
let dog1 = new Dog("Buddy", "Golden Retriever");
dog1.bark();

In the below PDF we discuss about OOPs Concept in JavaScript in detail in simple language, Hope this will help in better understanding.

JavaScript

OOPs Concept in JavaScript:

The four pillars of OOP are Inheritance,Encapsulation, Abstraction and Polymorphism

1. Inheritance:
JavaScript supports inheritance through the extends keyword. Subclasses (child classes) can inherit properties and methods from a superclass (parent class).

class Animal {
constructor(name) {
this.name = name;
}
makeSound() {
console.log("Some generic sound");
}
}
class Cat extends Animal {
makeSound() {
console.log("Meow");
}
}
let myCat = new Cat("Whiskers");
myCat.makeSound(); // Outputs "Meow"

2. Encapsulation:
Encapsulation involves bundling data and methods that operate on the data within a single unit, such as a class. This helps hide the internal implementation details and exposes a clean, well-defined interface.

class BankAccount {
constructor(balance) {
this._balance = balance; // Encapsulation by using a private property
}
getBalance() {
return this._balance;
}
deposit(amount) {
this._balance += amount;
}
withdraw(amount) {
if (amount <= this._balance) {
this._balance -= amount;
} else {
console.log("Insufficient funds");
}
}
}
let account = new BankAccount(1000);
account.withdraw(500);
console.log(account.getBalance()); // Outputs 500

3. Polymorphism:
Polymorphism allows objects to be treated as instances of their parent class, enabling flexibility in code design.

class Shape {
area() {
console.log("This method should be overridden");
}
}
class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}
area() {
return Math.PI * this.radius ** 2;
}
}
class Square extends Shape {
constructor(side) {
super();
this.side = side;
}
area() {
return this.side ** 2;
}
}
let circle = new Circle(5);
let square = new Square(4);
console.log(circle.area()); // Outputs the area of the circle
console.log(square.area()); // Outputs the area of the square

4. Abstraction:

Abstraction in programming refers to the concept of hiding the implementation details of an object and exposing only the essential features or functionalities.
You can achieve abstraction by encapsulating functionality within functions, allowing you to hide the implementation details while exposing a clean interface.

function calculateArea(shape) {
// Abstracted function for calculating area
if (shape.type === "circle") {
return Math.PI * shape.radius ** 2;
} else if (shape.type === "square") {
return shape.side ** 2;
} else {
return "Unsupported shape";
}
}
let circle = { type: "circle", radius: 5 };
let square = { type: "square", side: 4 };
console.log(calculateArea(circle)); // Outputs: 78.53981633974483
console.log(calculateArea(square)); // Outputs: 16

Related Question

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into reusable and modular units called objects. These objects encapsulate data and behavior, allowing for a more structured and efficient code organization.

JavaScript supports OOP through its object-based approach. It allows the creation of objects, which can have properties and methods, facilitating the organization of code using principles like encapsulation, inheritance, and polymorphism.

In JavaScript, an object is a composite data type that encapsulates data and behavior. Objects are created using curly braces {}, and properties and methods are added to them to represent real-world entities in the code.

Encapsulation in JavaScript OOP refers to the bundling of data (properties) and methods that operate on the data within a single unit, i.e., an object. This helps in hiding the internal details of an object and exposing only the necessary functionalities.

Relevant

JavaScript JSON JSON stands for

JavaScript Function Generator Function generators,

JavaScript WeakSet A WeakSet is

JavaScript WeakMap A WeakMap is

JavaScript Proxy A JavaScript Proxy

JavaScript Boolean JavaScript Boolean is

JavaScript BigInt JavaScript BigInt represented

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads
Your Poster