Creating an Object and it's method in JavaScript

Creating an Object and it's method in JavaScript

A JavaScript object is an entity having a state and behaviour (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.JavaScript is an object-based language. Everything is an object in JavaScript.JavaScript is a template based not class-based. Here, we don't create a class to get the object. But, we direct create objects.

Creating Objects in JavaScript

There are 3 ways to create objects.

  1. By object literal

  2. By creating an instance of Object directly

  3. By using an object constructor

1) JavaScript Object by object literal

syntax:

object={property1:value1,property2:value2.....propertyN:valueN}

As you can see, property and value is separated by : (colon).

example:

<script>  

emp={id:102,name:"Shyam Kumar",salary:40000}  

document.write(emp.id+" "+emp.name+" "+emp.salary);  

</script>

Output :

102 Shyam Kumar 40000

2) By creating instance of Object

syntax:

var objectname=new Object();

Here, new keyword is used to create object.

example:

<script>  

var emp=new Object();  

emp.id=101;  

emp.name="Ravi Malik";  

emp.salary=50000;  

document.write(emp.id+" "+emp.name+" "+emp.salary);  

</script>

Output:

101 Ravi 50000

3) By using an Object constructor

Here, you need to create a function with arguments. Each argument value can be assigned in the current object by using this keyword.

The "this" keyword refers to the current object.

example:

<script>  

function emp(id,name,salary){  

this.id=id;  

this.name=name;  

this.salary=salary;  

}  

e=new emp(103,"Vimal Jaiswal",30000);  

document.write(e.id+" "+e.name+" "+e.salary);  

</script>

Output:

103 Vimal Jaiswal 30000

Defining method in JavaScript Object

We can define method in JavaScript object. But before defining the method, we need to add a property in the function with the same name as a method.

example:

<script>  

function emp(id,name,salary){  

this.id=id;  

this.name=name;  

this.salary=salary;  



this.changeSalary=changeSalary;  

function changeSalary(otherSalary){  

this.salary=otherSalary;  

}  

}  

e=new emp(103,"Sonoo Jaiswal",30000);  

document.write(e.id+" "+e.name+" "+e.salary);  

e.changeSalary(45000);  

document.write("<br>"+e.id+" "+e.name+" "+e.salary);  

</script>

output:

103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000

JavaScript Object Methods

The various methods of Object are as follows:

S.NoMethodsDescription
1Object.assign()This method is used to copy enumerable and own properties from a source object to a target object
2Object.create()This method is used to create a new object with the specified prototype object and properties.
3Object.defineProperty()This method is used to describe some behavioral attributes of the property.
4Object.defineProperties()This method is used to create or configure multiple object properties.
5Object.entries()This method returns an array with arrays of the key, value pairs.
6Object.freeze()This method prevents existing properties from being removed.
7Object.getOwnPropertyDescriptor()This method returns a property descriptor for the specified property of the specified object.
8Object.getOwnPropertyDescriptors()This method returns all own property descriptors of a given object.
9Object.getOwnPropertyNames()This method returns an array of all properties (enumerable or not) found.
10Object.getOwnPropertySymbols()This method returns an array of all own symbol key properties.
11Object.getPrototypeOf()This method returns the prototype of the specified object.
12Object.is()This method determines whether two values are the same value.
13Object.isExtensible()This method determines if an object is extensible
14Object.isFrozen()This method determines if an object was frozen.
15Object.isSealed()This method determines if an object is sealed.
16Object.keys()This method returns an array of a given object's own property names.
17Object.preventExtensions()This method is used to prevent any extensions of an object.
18Object.seal()This method prevents new properties from being added and marks all existing properties as non-configurable.
19Object.setPrototypeOf()This method sets the prototype of a specified object to another object.
20Object.values()This method returns an array of values.