How OOPS concept works in JavaScript

Posted By :Parwez Alam |30th September 2022

OOP, or object-oriented programming, is a paradigm for programming that is built on the idea of objects;

Using objects, we can represent concrete or abstract qualities;

 

In OOP, objects are self-contained pieces/blocks of code. OOP was developed to organize code, to make it more flexible and easier to maintain(avoid “spaghetti code”).

 

To create the new objects programmatically we use classes for that. You can think of a class is like a blueprint that can be then used to create a new object based on a rule, described in the class. So it’s just like a blueprint from which we can create multiple new objects. For e.g from a single blueprint, we can create multiple houses.

 

 

How do we design classes?

 

To design classes, there is not a single best technique for designing classes. There are however four fundamental principles that can guide us towards a good class implementation, and these principles are:-

 

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

 

1. Abstraction:- Ignoring or hiding details that don’t matter, allowing us to get an overview perspective of the thing we are implementing, instead of messing with details that don’t matter to our implementation.

 

Abstraction is really important not just only in OOP but in programming in general. We create and use abstraction all the time.

 

 

2. Encapsulation:- Is the practice of keeping methods and properties private inside the class, so they are not accessible from outer classes. Some methods can have a public interface (API).

 

With this critical property, we prevent external code from accidentally manipulating internal properties/state(Object Data) which can reduce the chances of bugs, especially for large code. To make it encapsulated we use private before property that will not be accessible from outside but still accessible from within the class.

 


 

3. Inheritance:- Establishing a hierarchical relationship between classes by making all of a class's properties and methods accessible to its child class. This enables us to reuse common logic and model relationships seen in the actual world.

 

 

4. Polymorphism:- A child can overwrite a method initiated from a parent class.

 

JavaScript is not a class-based object-oriented language. However, it still uses some sorts of object-oriented programming (OOP).

 

In JavaScript, we have something called Prototype. In JavaScript, each object is connected to a particular prototype object. This means each object has a prototype.

 

The prototype contains methods (behavior) that are accessible to all objects linked to that prototype, this is called prototypal inheritance.

 

There are 3 ways to implement prototypal inheritance in Javascript (or how can we create new objects, without having classes):-

 

1. Constructor Function

2. ES6 Classes

3. Object. create()

 


About Author

Parwez Alam

Parwez is a seasoned Frontend Developer with several years of industry experience. He specializes in a wide range of technologies including Angular, JavaScript, jQuery, Bootstrap, CSS, and HTML. Additionally, he has a good understanding of Figma design basics, which adds to his skillset. He has made significant contributions to projects such as the Pnp-Park App, the KRB project and Konfer project, where he brings his expertise to deliver outstanding solutions. One of his notable traits is, keen interest in learning new technologies to meet the specific requirements of each project.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us