Object-Oriented Programming ( OOP ) in JavaScript – Concept


In this article, we will take a look at what is Object-Oriented Programming. We will understand what is the use of OOP in javascript and how to use it. we will also understand how to declare a class and its properties and methods. In the previous article, we learn what is Map() Function. If you wanted to read this article first then go ahead.  but first, before we move forward we need to understand what is OOP stands for.

Object-Oriented Programming

OOP stands for object-oriented programming. To understand object-oriented programming where you need to first understand what is an object.

The object is a simply an unordered collection of properties. Each of which has a name and value. The object can aggregate multiple values and allows you to store and access that value by property name. The property name can be a string or a symbol.

Now we will understand what is the use of the object in object-oriented programming. An object-oriented programming is a very important aspect in every programming language. I really hope that you carefully read all the instruction and understand what is object-oriented programming.

In object-oriented programming, an object Is a logically related Collection of data and functionality. Now this object can be a collection of properties and methods. Using the real world example you will completely understand how the object is really created.

For example,

Now let’s assume that an object is a car and a car has multiple parts And functionality. Now here we will assume car parts as properties and functionality as methods. it means if your car has a Model number,  number of doors, glasses, number of wheels.  these all parts are properties of the car object. Whereas,  open door. Accelerate, Turn on headlights and every functionality of the car are methods of car objects.

Now, this piece of information will complete the car. It means when you combine car properties and methods then you will get your complete usable car. So now the object also have the same similarity as a car. Using properties you can specify object parts and using methods you can specify object functionality.

Now you know that to build a complete car you will need properties and methods.

Object Reference

Now here I also want to introduce a one more important thing is you can access object reference anytime using reference. it means when you specify your object reference to the other variable then you can modify this object using the reference variable.

Using this example you will understand what I want to say. Suppose you have an object and you have a reference of that object in the variable.

like this.

So when you execute this statement you can see your object has one property created using the reference variable.

Class

we will understand how to create a class in JavaScript and how to use it. In the previous lecture will learn what is object-oriented programming. Now in this tutorial, we will explore a new topic class. Prior to ECMAScript 6 creating  a class is very fussy. Ecmascript 6 comes with the new convenient syntax of class.

This Syntax creates a new class model. So when you want to access a model class then you need to create an instance of the class. No instance has been created yet. So we need to create a class instance first to access class properties and methods.

To create a class instance we will just say,

So now we have two instances of the modal class. When you creating an instance of the class you need to use a new keyword.  the new keyword will create a new instance of the class. now using Obj1 and obj2  you can access object properties and methods.

we did not create any properties and methods yet.  but we will look at them after some timeBut first, before we make the class sophisticated I wanted to introduce you an instance of Keyword which tells you that which instance belongs to which class.

Now if you wanted to find out about which instance belongs to which class than an instance of Keyword help you out.

For example,

As I said in the previous lecture you can create properties and methods of the class. Now let’s make this class more interesting.

Explore class

Now the constructor is used to initialize class properties. The constructor is a method which invokes when the class instance is created. So when you create an instance of the class you need to specify parameter if you create a constructor with arguments. So here in this example we had created constructor with two arguments. we need to specify argument value when we we create an instance of the class. In the constructor, we had use this keyword.

Now here this keyword refers to the instance of the object. It means here year in the constructor we create two properties of the modal class using this keyword. In the previous lecture will learn how to create a property using an instance. So here we are using this keyword to create a new property of the class.

so this keyword refers to the instance of the class. after that, we have created the show method. To create a method in the class you just need to specify method name with parenthesis.  and just after that specify curly braces and the body of the method. The method is just like a function. The method is created for adding functionality to your class.

Right.

When you execute this statement you will get your model number and you are a model name on the console. it using the class you can solve any problems in programming.

That’s it. I hope you understand this article. we write more article to understand javascript well. So stay tune and keep reading articles.