JavaScript – Objects


What is an object in OOPs ?

Objects are also called an instance of the class which can be a combination of variables, methods and other members.

What is an object in javascript ?

In Javascript, an object is an entity having a state and behaviour (properties and method).

Everything is an object in Javascript.

Since Javascript is not class based, we do not create a class to get an object.

We can directly create objects in javascript.

There are three different ways to create objects.

1. By Object Literal

2. By Creating the instance of the object

3. By Object constructor

Javascript online tutorial !!!

Click to Learn More about – Javascript online learning

1. By Object Literal

We can create objects by object literals.

Syntax :

object = {prop1:value1,prop2:value2,prop3:value3...propN:valueN};

object – This is the object name.


prop – This is the property name of the object.

value – This is the value assigned to the property.

Example :

<script>
			var obj = {name:"Abdul",age:"33",address:"Delhi"};
			document.writeln("Name = " + obj.name+"<br/>");
			document.writeln("Age = " + obj.age + "<br/>");
			document.writeln("Address = " + obj.address+"<br/><br/>");
			var obj1 = {name:"Rahul",age:"43",address:"Chennai"};
			document.writeln("Name = " + obj1.name+"<br/>");
                        document.writeln("Age = " + obj1.age + "<br/>");
                        document.writeln("Address = " + obj1.address+"<br/><br/>");
		</script>

Output :

2. By Creating the instance of the object

We can create objects directly by using “new” keyword.

Syntax :

var ObjectName = new ObjectName();

ObjectName – This is the object name.

new – This is a javascript reserved keyword.


Example :

<script>
			var obj = new Object();
			obj.name = "Abdul";
			obj.age = "33";
			obj.address = "Delhi";
			document.writeln("Name = " + obj.name+"<br/>");
			document.writeln("Age = " + obj.age + "<br/>");
			document.writeln("Address = " + obj.address+"<br/><br/>");
			var obj1 = new Object();
			obj1.name = "Rahul";
			obj1.age = "43";
			obj1.address = "Chennai";
			document.writeln("Name = " + obj1.name+"<br/>");
                        document.writeln("Age = " + obj1.age + "<br/>");
                        document.writeln("Address = " + obj1.address+"<br/><br/>");
		</script>

Output :

3. By Object constructor

Here object values are assigned by using “this” keyword.

We are creating a function and while creating an object, all the function variables are assigned using “this” keyword.

Example :

<script>
			function getInfo(name,age,address){
				this.name = name;
				this.age = age;
				this.address = address;
			}
			info = new getInfo("Abdul","87","Chennnai");
			document.writeln("Student Info <br/>");	
			document.writeln("Name = "+info.name+"<br/>");	
			document.writeln("Age = "+info.age+"<br/>");	
			document.writeln("Address = "+info.address+"<br/><br/>");	
			info = new getInfo("Mark","72","Pune");
			document.writeln("Student Info <br/>"); 
                        document.writeln("Name = "+info.name+"<br/>");  
                        document.writeln("Age = "+info.age+"<br/>");    
                        document.writeln("Address = "+info.address+"<br/><br/>");    
			info = new getInfo("Ramesh","23","Mumbai");
                        document.writeln("Student Info <br/>"); 
                        document.writeln("Name = "+info.name+"<br/>");  
                        document.writeln("Age = "+info.age+"<br/>");    
                        document.writeln("Address = "+info.address+"<br/><br/>");    
		</script>

Output :

We can define multiple methods inside the javascript objects, but proper calling of method will give us the desired output.

Some of the method objects are listed below.

SnoMethodDescription
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 behavioural 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 is 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 extension 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.