Objects in JavaScript

In JavaScript an object is a real time entity having properties and method. For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object oriented programming language. Everything is an object in JavaScript.

JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.

Creating Objects in JavaScript

There are 3 ways to create objects.

1- It can created by object literal
2- It can created by creating instance of Object directly (using new keyword)
3- It can created by using an object constructor (using new keyword)


JavaScript Object by object literal

The syntax of creating object using object literal is given below:
object={property1:value1,property2:value2.....propertyN:valueN} As you can see, property and value is separated by : (colon).
Let’s see the simple example of creating object in JavaScript.

<script type = "text/javascript">
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);  
</script>                  
        

Output of above code


By creating instance of Object

The syntax of creating object directly is given below:
var objectname=new Object(); Here, new keyword is used to create object.
Let’s see the example of creating object directly.

<script type = "text/javascript">
var emp=new Object();  
emp.id=101;  
emp.name="Ravi Malik";  
emp.salary=50000;  
document.write(emp.id+" "+emp.name+" "+emp.salary);    
</script>                  
        

Output of above code


By using an Object constructor

Here, you need to create 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.
The example of creating object by object constructor is given below.

<script type = "text/javascript">
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 of above code


JavaScript Function Methods

Let's see function methods with description.

Method Description
Object.assign() This object method is used to copy enumerable and own properties from a source object to a target object in JavaScript.
Object.create() This object method is used to create a new object with the specified prototype object and properties in JavaScript.
Object.defineProperty() This object method is used to describe some behavioral attributes of the property in JavaScript.
Object.defineProperties() This object method is used to create or configure multiple object properties in JavaScript.
Object.entries() This object method returns an array with arrays of the key, value pairs in JavaScript.
Object.freeze() This object method prevents existing properties from being removed in JavaScript.
Object.getOwnPropertyDescriptor() This object method returns a property descriptor for the specified property of the specified object in JavaScript.
Object.getOwnPropertySymbols() This object method returns an array of all own symbol key properties in JavaScript.
Object.getPrototypeOf() This object method returns the prototype of the specified object in JavaScript.
Object.is() This object method determines whether two values are the same valuein JavaScript.
Object.isExtensible() This object method determines if an object is extensible in JavaScript.
Object.isFrozen() This object method determines if an object was frozen in JavaScript.
Object.keys() This object method returns an array of a given object's own property names in JavaScript.
Object.preventExtensions() This object method is used to prevent any extensions of an objectin JavaScript.
Object.seal() This object method prevents new properties from being added and marks all existing properties as non-configurable in JavaScript.
Object.setPrototypeOf() This object method sets the prototype of a specified object to another object in JavaScript.
Object.isSealed() This object method determines if an object is sealed in JavaScript.
Object.values() This object method returns an array of values in JavaScript.







The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.