JavaScript OOP Course 1: Introduction
References
- Code with Mosh: The Ultimate JavaScript Mastery Series – Part 2
- W3School: JavaScript Tutorial
What is OOP
Object Oriented Programming is a programming paradigm (or style of programming) centered around Objects rather than Functions.
Four Pillars of OOP
The four core concepts of Object Oriented Programming:
1. Encapsulation – OOP comes to solve the spaghetti problem of the function orientated programming. OOP combine a group of related variables and functions into into a unit and we call that unit Object. The Object's variables are called Properties and the Object's functions are called Methods.
2. Abstraction – hide the complexity inside the Object and create a simple interface with only few Properties and Methods to interact with. Thus we can reduce the impact of change of the internal Object's logic.
3. Inheritance – it allows (helps) you to eliminate redundant code. In OOP we can define some Properties and Methods into a generic object and have may Objects inherited these (generic) Properties and Methods.
4. Polymorphism – poly means many and morph means form => so polymorphism means many forms. In OOP polymorphism is a technique that allows you to get rid of long if and else or switch and case statements. For example within the generic Object we can define one method that will behave differently depending of the type of the Object we have referencing.
The benefits of of Object Oriented Programming:
1. With the Encapsulation we group related variables and functions together and in this way we can reduce the complexity. We can reuse the objects in different parts of a program or in different programs. This method reduces the complexity and increase the reusability.
2. With the Abstraction we can hide the details and complexity and show only the essentials . This method reduces the complexity and isolate the impact of changes of the code.
3. With the Inheritance we can eliminate the redundant code.
4. With the Polymorphism we can refactor the ugly switch/case statements.