Object-Oriented Programming Paradigm
If you are familiar with basic Javascript, then you might know about two paradigms of Javascript.
- Functional programming paradigm
- Object-Oriented Programming Paradigm
In this article, we’ll discuss the Object-Oriented Programming Paradigm. Almost everything in Javascript is an Object. that’s why it’s so important to understand it. If you understand, then you most likely able to read other’s code.
Object-oriented programming(OOP) is a way of structuring your code for optimal readability, use, and maintenance. There are some basic characteristics of OOP that we must know:
- In OOP, we program in terms of Classes, Object, methods, properties etc.
- OOP integrates terms such as Abstraction, Encapsulation, Modularity, Privacy, Polymorphism, Inheritance etc.
Inheritance
Inheritance is a very important characteristic of OOP. In ES5, there is a Factory pattern used for the Inheritance in OOP.
Factory Pattern
let’s discuss with an example:
As you can see in the above example, Employee is inheriting the properties and methods of Person. Since Function is an Object, whenever you create a function, there is a property called Prototype, assigns to it, which is also an Object. This Object has a default method constructor which return that function itself. so basically, In the above example, Person has their own prototype where we assign some other properties like eyes, mouth, and sleep. and Employees must have all property of a Person, so they want to inherit these properties from their prototype and an employee can also have their own property like salary. So, when an Object inherits the properties from another Object, this is called Prototypal Inheritance.
ES6 Class Pattern
In ES6, it’s easy to inherit the property from another Object using Class pattern. you can use class keyword and for Inheritance, you can extend to the particular Object from where you want to Inherit. like this:
class Employee extends Person {
costructor() {}
}
let’s discuss the above example with the Class pattern:
Employee Object extends the Person’s prototypes as you can see in the above example. it can create their own properties and methods and Descendent class may redefine the method of a parent’s Object, and this is called Polymorphism.
You can use both the pattern for the Inheritance. but Class Inheritance is totally different from Prototypal Inheritance.
If you have any concern regarding this article, feel free to comment on it.
you can also find me on Twitter: https://twitter.com/ashwanigg3