Object Oriented Programming(part 1) : Every JavaScript developer must know

Rhea RB
2 min readApr 15, 2023

--

Object oriented programming is a programming language paradigm that is based on the concept of objects

  1. which can contain data in the form of properties and code in the form of methods.
  2. OOPs is a popular programming paradigm because it allows for modular, reusable code that is easier to maintain, read and scale.

Features of OOPS

There are four rules or main pillars of object oriented programming language. This defines how the data and actions associated with the data are organized using code.

OOPS concepts

  1. Objects
  2. classes
  3. inheritance
  4. polymorphism
  5. encapsulation
  6. abstraction

We will discuss Class and Object in this blog in detail.

Class

In JavaScript a class is a blueprint for creating objects with specific properties and methods.

  1. It provides a way to define the structure and behavior of an object which can then be used to create multiple instances of that object
  2. The JavaScript class contains various class members within a body including methods or constructor.
  3. There is two steps to use class:
    1. create a class
    2. Create individual objects

Create a class

In JavaScript we create a class using the class keyword, followed by the name of the class.
Here is an example:

class Cars {
constructor(name){
this.name = name
}

getName() {
console.log(`${this.name} is my new car`
}
}

Constructor Method
A constructor is a special method that gets called when an object is created from a class.

It allows us to set the initial values of the objects properties.

  1. In previous example, we defined a class class cars, with a constructor method that takes in name and assigns it to the corresponding properties of the object.
  2. We have also defined a method named getName().

Using class in JS

Once we have defined our class we can use it to create individual objects.

Here is an example of how we can create an instance of the class. |

let myCar = new Cars('BMW')

We have created a new Car Object called myCar using the new keyword and passed the value of name.

We can call the methods of the object using the dot notation

myCar.getName()

Why use Classes ?

Classes are useful because they allow us to create objects with similar properties and behaviors in a more efficient and organized way

Instead of defining each object individually we can create a class that encapsulates the common properties and behaviors of those objects.

This also makes our code easier to maintain and update.

If you liked my blog, please do follow me on Rhea RB

Thank you for reading ! Happy Coding !!

--

--

Rhea RB

Hello ! I am a senior software engineer, passionate about tech and product.