OOP — Object Oriented Programming

Today is day 9 of Dev League and I’ve already learned tons! We have been learning about Object Oriented Programming for the past two days, so now I will do my best to explain to you what OOP is like a five year old.

First off, why would we want to use OOP? There are couple of reasons:

  • We can organize our code
  • Easier for code to make sense to you
  • Promotes the DRY(Don’t Repeat Yourself) principle
  • Promotes encapsulation(1 of the 4 pillars of OOP)

During our first week, we were learning about the imperative paradigm of Javascript where we used a sequence of statements to reach a goal. Whereas in OPP, we can make a structure that makes more sense to us to read. Before we move on, you need to know about the 4 pillars of OOP:

Abstraction

Exposes relevant functions. Reduces code complexity and irrelevant detail, which makes your code aesthetically pleasing.

Encapsulation

The process of putting in data and functions in a class(blueprint). This data is now hidden and can’t be accessed by anyone else.

Inheritance

Pretty much means, that you can create a children class that inherit s its properties and methods from a parent class.

Polymorphism

The word polymorphism means that you can change into different stages. That means that a children class can have properties and methods of its own, but still keep properties of its parent class.


So there are two things that make up OOP, classes and Objects:

Classes are like blueprints for how objects are structured and how they should behave.

Objects are instance of class , so whatever objects that are constructed from the classes all share the same property and methods.


Classical inheritance vs. Prototypical Inheritance

Our first introduction to OOP was through the new version of Javascript, ES6. Since, ES6 is new, we eventually had to learn OOP the older way, through prototypes.

In classical inheritance, when you create a class and then instantiate it, the instance is now divorced from the class.

Whereas in prototypical inheritance, adding a new prototype to a class will affect all instances of the class even if the its already been instantiated. Every object in JavaScript has a prototype, so that means that every object and prototype inherits from the main Object.