Object-Oriented Programming in JavaScript
Object-oriented programming in JavaScript is a programming paradigm that uses objects to model real-world things and relationships. It revolves around four core concepts: encapsulation, inheritance, polymorphism, and abstraction.
1. Encapsulation
Encapsulation refers to bundling the data (properties) and the methods (functions) that operate on the data into a single unit or class. This hides the internal state of the object and only exposes a controlled interface.
Example:
javascript
Copy code
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const person1 = new Person(‘Alice’, 30);
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old.
2. Inheritance
Inheritance is a mechanism where a new class (child class) derives properties and behaviors (methods) from an existing class (parent class). This allows for code reusability.
Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog(‘Rex’);
dog.speak(); // Output: Rex barks.
3. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name. It enables a single interface to represent different types.
Example:
class Bird {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} chirps.`);
}
}
class Parrot extends Bird {
speak() {
console.log(`${this.name} talks.`);
}
}
const bird = new Bird(‘Sparrow’);
const parrot = new Parrot(‘Polly’);
bird.speak(); // Output: Sparrow chirps.
parrot.speak(); // Output: Polly talks.
4. Abstraction
Abstraction means hiding the complex implementation details and showing only the essential features of the object. This simplifies the interaction with objects and makes the code more modular and manageable.
Example:
class Car {
constructor(brand) {
this.brand = brand;
}
start() {
console.log(`${this.brand} car started.`);
}
// Abstracted method to check engine status
checkEngine() {
// Complex logic hidden from the user
console.log(‘Checking engine status…’);
}
}
const myCar = new Car(‘Toyota’);
myCar.start(); // Output: Toyota car started.
myCar.checkEngine(); // Output: Checking engine status…
For Free Demo classes Call: 020-71173125
Registration Link: Click Here!
5. Prototypes and Classes
In JavaScript, before the introduction of classes in ES6, prototypes were used to achieve OOP. Even now, under the hood, JavaScript uses prototypes for inheritance.
Example using Prototypes:
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(`${this.name} makes a sound.`);
}
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.`);
}
const dog = new Dog(‘Rex’);
dog.speak(); // Output: Rex barks.
Summary
- Encapsulation: Bundling data and methods that operate on the data within one unit (class).
- Inheritance: Mechanism where one class inherits properties and methods from another class.
- Polymorphism: Ability to define methods in different ways for different objects.
- Abstraction: Hiding the complex implementation details and exposing only the necessary parts.
Must watch our video on Demand For Full Stack Developers in Future
Author:-
Sonal Vanarse
Call the Trainer and Book your free demo Class for JavaScript Call now!!!
| SevenMentor Pvt Ltd.
© Copyright 2021 | SevenMentor Pvt Ltd.