OOPs In Javascript

Is JavaScript an Object-Oriented Language?

Yes , Javascript is an Object-Oriented language , as we can implement all the Object-Oriented Paradigms in Javascript.

Now, Let’s start with understanding What OOPs actually is ?

OOPs stands for Object Oriented Programming .

Let’s break the above full form and try to understand it in a simple way. So, Object is an entity or a thing that possesses behavior and characteristics. In terms of programming language , these characteristics are the properties and the behavior is the function.

When we implement the Object Oriented Design to the language we create a program that is oriented around the object and we use the object’s characteristics and behaviors to solve the problem.

Creating an object in Javascript:

There are three ways of creating an object in Javascript:

  1. Using an Object Literal
  2. Using Object Class
  3. Using Constructor Function

We will discuss here the one basic approach and learn some interesting facts about it and then the other two approaches in the next post

  1. Creating an object using Object Literal:
var person ={
first_name: "John",
last_name: "Baker",
print: function()
   {
      alert("My first name is : "+this.first_name+" and last name is 
      "+this.last_name);
   }
}

So, as we see in the code above , object is similar to a variable , it is just that it possesses its own properties and functions.

first_name and last_name are the properties of a person and print is a function or you can say that it is a behavior that person object possesses.

You can access the properties and functions of an object using the dot(.) operator.

In the above code snippet we have used this keyword to access the properties of the object inside the print() function. It is mandatory to use “this” keyword to access the properties of the object inside the function else it will give you an error (first_name not defined), because the browser wouldn’t know from where to get this first_name, so you have to tell the browser that you want the first_name property of the the object who is also the owner of the print() function. So, to get the owner of the function you use “this” keyword which refers to the object under which the function is defined.

Now, to call the print function to alert the first_name and last_name of the person on the browser window , we will write the following code outside of the object :

var person ={
first_name: "John",
last_name: "Baker",
print: function()
   {
      alert("My first name is : "+this.first_name+" and last name is 
      "+this.last_name);
   }
}
person.print();

Some important points to note regarding the object creation:

  1. You cannot create variables inside an object it will give you an error.
  2. You cannot access the properties of an object inside the function defined in the same object without using the ‘this’ keyword.

The other two approaches to create an object will be discussed in the next post.

Leave a Reply

Your email address will not be published.