JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page

Was this helpful?

  1. OOP

4 principles of OOP

Encapsulation:

We wrap code into boxes related to one another. This box can interact with each other using the methods and properties that we make it available.

This makes the code easier to maintain and reusable.

Ex:
const person = {
    firstName: 'Vijay',
    lastName: 'Deepak',
    getName() {
        return `${this.firstName} ${this.lastName}`;
    }
};

Abstraction:

Hiding the complexity from the user.

Ex:
function complexFn(data) {
    // complex code & bussiness logics
}

complexFn("hi"); // code of the function is not considered

Inheritance:

Avoid code rewrite and save memory spaces with shared methods.

Ex:
class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    getFullName() {
        return `${this.firstName} ${lastName}`;
    }
}

class Men extends Person {
    constructor(firstName, lastName) {
        super(firstName, lastName);
    }
}

const vijay = new Men('Vijay', 'Deepak');
vijay.getFullName(); // Vijay Deepak

Polymorphism: ( Many Forms )

An ability to call a same method in different object, in each object responding different way. This is also known for Method overriding and Method Overloading.

Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

Overloading a method (or function) is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters).

Ex:
class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    getFullName() {
        return `${this.firstName} ${lastName}`;
    }
}

class Men extends Person {
    constructor(firstName, lastName) {
        super(firstName, lastName);
    }
    
    getFullName() {
        return `Sorry I can't`;
    }
}

const vijay = new Men('Vijay', 'Deepak');
vijay.getFullName(); // Sorry I can't
PreviousOOPNextFP

Last updated 5 years ago

Was this helpful?