JavaScript

Last Updated: 2/2/2023

OOP Concepts

  • Object-oriented programming is a programming paradigm or style of programming that is centered around objects rather than functions.
  • Tools, frameworks and languages come and go, object-oriented programming is still very relevant today because it's not a programming language or tool. It's a style of programming or a programming paradigm.
  • There are several programming languages out there that support Object-oriented programming such as C#, Java, Ruby, Python, JavaScript, and more.

Procedural Programming

  • Divides a program into a set of functions
  • You have data stored in a bunch of variables and functions that operate on the data. This style of programming is very simple and straight forward,
  • As your programs grow, you will end up with a bunch of functions that are all over the place. You might find yourself copying and pasting lines of code over and over
  • Make a change to one function and then several other functions break. This is called spaghetti code, there is so much interdependency in all these functions it becomes problematic.

Four Pillars of OOP

Four core concepts in object-oriented programming are

  • encapsulation,
  • abstraction,
  • inheritance, and
  • polymorphism

Encapsulation

  • In object-oriented programming you combine a group of related variables and functions into a unit (object). This is called Encapsulation.
  • You refer to these variables as properties, and the functions as methods.
  • You can reduce complexity.
  • You can reuse this object in different parts of the program, or in different programs.

Abstraction

  • Hide the details and the complexity (properties and methods) and show only the essentials.
  • Reduces Complexity because the interface of the objects is simpler.
  • Reduce the Impact of Change because changing the code (private members) will not impact the rest of the code.
  • Example DVD player. DVD player has a complex logic board on the inside and few buttons on the outside to interact with. You simply press the play button, and you don't care what happens on the inside. All that complexity is hidden from you. This is abstraction in practice.

Inheritance

  • Inheritance is a mechanism that allows you to eliminate redundant code.
  • Example In html you have elements like TextBox, drop-down lists, checkboxes and so on. All these elements have a few things in common. They have properties like hidden, and innerHTML and methods like click and focus. Instead of redefining all these properties and methods for each HTML element, you can define them in a generic object HTMLElement and have other objects inherit these properties and methods.

Polymorphism

  • Poly means many, morph means form. Many Form
  • A method will behave differently depending on the type of the object you are referencing
  • Polymorphism is a technique that allows you to get rid of long if else or switch and case statements.
  • Example In HTML elements textbox, checkbox should have the ability to be rendered on a page. But the way each element is rendered is different from the others. With object-orientation you can implement a render method in each of these objects, and the render method will behave differently depending on the type of the object we are referencing.

Summary

  • Using encapsulation, you group related variables and functions together. This technique reduces complexity and increases reusability.
  • With abstraction, you hide the details and the complexity and show only the essentials. This technique reduces complexity, and also isolates the impact of changes in the code.
  • With inheritance we can eliminate redundant code,
  • With polymorphism, you can refactor ugly switch case or if else statements.