Home >>Javascript Tutorial >JavaScript Objects
The objects in JavaScript is a structure that has a state and behavior or we can say properties or method just like, Bike, pen, car, etc.
It is a known fact that JavaScript is an object-based language and every single thing in JavaScript is called an Object.
In JavaScript, class is not created in order to get the object instead, the object is created directly hence, making it a template-based language.
These three ways are followed to create objects in JavaScript:
Here is the syntax for creating object by Object Literal:
object={property1:value1,property2:value2.....propertyN:valueN}
Please note that the property and value is separated by : (colon).
Let's understand this method of object creation by this simple example:
<script> emp={id:101,name:"Radhe Shyam",salary:120000} document.write(emp.id+" "+emp.name+" "+emp.salary); </script>
The output of the above mentioned example is: 101 Radhe Shyam 120000
Here is the syntax of creating object directly:
var objectname=new Object();
Please note that new keyword is used here to create object.
Let's understand the object directly with this example:
<script> var emp=new Object(); emp.id=130; emp.name="Hello Employee"; emp.salary=90000; document.write(emp.id+" "+emp.name+" "+emp.salary); </script>
The output of the above mentioned example is: 130 Hello Employee 90000
In this method functions needs to be created with arguments and each argument value can be assigned easily in the current object by the help of this keyword. Please note that ‘this’ keyword is referring to the current object.
Here is an example to understand this easily:
var objectname=new Object();
Please note that new keyword is used here to create object.
Let's understand the object directly with this example:
<script> function emp(id,name,salary){ this.id=id; this.name=name; this.salary=salary; } e=new emp(11,"hello hi",65000); document.write(e.id+" "+e.name+" "+e.salary); </script>
The output of the above mentioned example is: 11 hello hi 65000
A method can be defined in JavaScript Object but in order to define method a property needs to be added in the function with the same name as of method. Here is an example to explain it better:
<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(10,"oonu aiwal",5000); document.write(e.id+" "+e.name+" "+e.salary); e.changeSalary(56000); document.write(" "+e.id+" "+e.name+" "+e.salary); </script>
The output for the above mentioned example is:
10 oonu aiwal 5000
10 oonu aiwal 56000
Here are the following various object methods in JavaScript:
Methods | Description |
---|---|
Object.assign() | This method copies own properties from a source object to a target object |
Object.create() | It is used to create a new object with the specified prototype object and properties |
Object.defineProperty() | It is used to describe some behavioral attributes of the property |
Object.defineProperties() | It is used to create or configure multiple object properties |
Object.entries() | It returns an array with arrays of the key, value pairs |
Object.freeze() | It prevents existing properties from being removed |
Object.getOwnPropertyDescriptor() | It returns a property descriptor for a specified property of a specified object |
Object.getOwnPropertyDescriptors() | It returns all own property descriptors of a given object |
Object.getOwnPropertyNames() | It returns an array of all properties (enumerable or not) found |
Object.getOwnPropertySymbols() | It returns an array of all own symbol key properties |
Object.getPrototypeOf() | It returns the prototype of the specified object |
Object.is() | It determines whether two values are the same value |
Object.isExtensible() | It determines if an object is extensible |
Object.isFrozen() | It determines if an object was frozen |
Object.isSealed() | It determines if an object is sealed |
Object.keys() | It returns an array of a given object's own property names |
Object.preventExtensions() | It is used to prevent any extensions of an object |
Object.seal() | It prevents new properties from being added and marks all existing properties as non-configurable |
Object.setPrototypeOf() | It sets the prototype of a specified object to another object |
Object.values() | It returns an array of values |