Objects in JavaScript

In this tutorial, we will learn about Objects in JavaScript. JavaScript is an Object-Oriented based language. Because programmers can perform basic oops concepts in code while working in JavaScript. But there is some difference as compared to other object-oriented programming languages. Because in JavaScript we don’t create classes. Because we create objects directly. it means you can say its template-based language not class-based. We will learn several things in this tutorial.

  • Creating an Object
  • Defining method Object
  • Object Method

Creating an Object:

To use Object, we need to create an object first so create an object we have three methods by using these we can create an object. These methods are as follow:

  • Object literal
  • Creating an object directly with the help of a new keyword.
  • Using constructor

Now we will check each in detail:

Object literal: To create an object using object literal we just need to write object name then we have to define all properties using property name followed by the value. Follow this example to understand it more clearly.

<script>  
car={color:"Red",fuel:"Petrol",type:"SUV"}  
document.write(car.color+" "+car.fuel+" "+car.type);  
</script>

Creating an object directly with the help of a new keyword: In the last example, we learnt how we can create the object directly. Here we will create an object using the new keyword. Follow this example to understand it.

<script>  
var car=new Object();  
car.color="Red";  
car.fuel="Petrol";  
car.type="SUV";  
document.write(car.color+" "+car.fuel+" "+car.type);  
</script>

Using constructor: In this type first we create function. Then we define all the properties of an object in that function. To understand it more clearly follow this example.

<script>  
function car(color,fuel,type){  
this.color=color;  
this.fuel=fuel;  
this.type=type;  
}  
myCar=new car("Red","Petrol","SUV");  
  
document.write(myCar.color+" "+myCar.fuel+" "+myCar.type);  
</script>

Defining method in Object:

We can easily create a function in our JavaScript Object. Just check the following example to understand it more wisely.

<script>  
function student(id,name,fee){  
this.id=id;  
this.name=name;  
this.fee=fee;  
  
this.feeSubmit=feeSubmit;  
function feeSubmit(submitAmount){  
this.fee=this.fee-submitAmount;  
}  
}  
stu1=new student(10,"Ravi Shankar",15000);  
document.write(stu1.id+" "+stu1.name+" "+stu1.fee);  
e.feeSubmit(10000);  
document.write("<br>"+stu1.id+" "+stu1.name+" "+stu1.fee);  
</script>

Object Method:

There are several methods for objects. List of these methods is as follow:

MethodsDescription
Object.assign()This method id use to copy properties from source object to target object.
Object.create()This method use to create Object in JavaScript.
Object.defineProperty()This method id use to define property in Object
Object.defineProperties()This method is used to create or configure multiple object properties.
Object.is()This method is use to check two values are same or not.
Spread the love