Object-Oriented Programming in JavaScript

Shivam Gupta
3 min readJun 25, 2019

--

There are certain features or mechanisms which makes a Language Object Oriented like:

  1. Object

2. Classes

3. Encapsulation

4. Inheritance

1. Object– An Object is a unique entity which contains property(keys) and methods. Objects are everywhere in JavaScript almost every element is an Object whether it is a function,arrays and string.

#3 Ways to define an Object

a. Create an object directly

b. Using an Object constructor

c. Using Object.create() method: The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.

2.Classes– Classes are blueprint of an Object. A class can have many Object, because class is a template while Object are instances of the class or the concrete implementation.
Before we move further into implementation, we should know unlike other Object Oriented Language there is no classes in JavaScript we have only Object

JavaScript is a prototype based object oriented language, which means it doesn’t have classes rather it define behaviors using constructor function and then reuse it using the prototype.

Note: Even the classes provided by ECMA2015 are objects.

Example:
Let's use ES6 classes then we will look into a traditional way of defining Object

ES6 Style classes Define — but internally it is a prototype based
Defining class in a Traditional Way.

As seen in the above example it is much simpler to define and reuse object in ES6. Hence, we would be using ES6 in all our examples.

3. Encapsulation — The process of wrapping property and function within a single unitis known as encapsulation. Encapsulation includes the idea that the data of an object should not be directly exposed

Encapsulation means information hiding. It’s about hiding as much as possible of the object’s internal parts and exposing a minimal public interface.

Most of the OOP languages provide access modifiers to restrict the scope of a variable, but there are no such access modifiers in JavaScript but there is a certain way by which we can restrict the scope of a variable within the Class/Object.

Defining a private function in JS

4. Inheritance — It is a concept in which some property and methods of an Object is being used by another Object.

Unlike most of the OOP languages where classes inherit classes, JavaScript Object inherits Object i.e. certain features (property and methods)of one object can be reused by other Objects.(geekForgeeks)

Example: Inheritance in javascript

If a child class doesn't have a getInfo method so Parent class getInfo will be called.

Note: The Person and Student object both have same method i.e getInfo(), this is called as Method Overriding. Method Overriding allows method in a child class to have the same name and method signature as that of a parent class.
In the above code, super keyword is used to refer immediate parent class instance variable.

Write in the comment section for any question or suggestion

Thanks for reading this Post. If you enjoy this Post please give a clap. Thanks :)

--

--

Shivam Gupta
Shivam Gupta

Written by Shivam Gupta

Full Stack Engineer (Web/App) working on different JS Technologies & frameworks— Angular, Node, Typescript, Ionic, Firebase, AWS, ElK...Love to write cool stuff

No responses yet