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 objects in JavaScript
  • 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.

document.write(car.color+" "+car.fuel+" "+car.type);  

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.

var car=new Object();  
document.write(car.color+" "+car.fuel+" "+car.type);  

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.

function car(color,fuel,type){  
myCar=new car("Red","Petrol","SUV");  
document.write(myCar.color+" "+myCar.fuel+" "+myCar.type);  

Defining method in Object:

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

function student(id,name,fee){  
function feeSubmit(submitAmount){  
stu1=new student(10,"Ravi Shankar",15000);  
document.write(stu1.id+" "+stu1.name+" "+stu1.fee);  
document.write("<br>"+stu1.id+" "+stu1.name+" "+stu1.fee);  

Object Method:

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

Methods Description
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
Scroll to Top