Objects of JavaScript

JavaScript Objects

A javaScript object is an entity having state and behavior (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 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. By object literal
  2. By creating instance of Object directly (using new keyword)
  3. By using an object constructor (using new keyword)

1) JavaScript Object by object literal

The syntax of creating object using object literal is given below:
  1. 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.
  1. <script>  
  2. emp={id:102,name:"Shyam Kumar",salary:40000}  
  3. document.write(emp.id+" "+emp.name+" "+emp.salary);  
  4. </script>  
Test it Now

Output of the above example

102 Shyam Kumar 40000


2) By creating instance of Object
The syntax of creating object directly is given below:
  1. var objectname=new Object();  
Here, new keyword is used to create object.
Let’s see the example of creating object directly.
  1. <script>  
  2. var emp=new Object();  
  3. emp.id=101;  
  4. emp.name="Ravi Malik";  
  5. emp.salary=50000;  
  6. document.write(emp.id+" "+emp.name+" "+emp.salary);  
  7. </script>  
Test it Now

Output of the above example

101 Ravi 50000

3) 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.
  1. <script>  
  2. function emp(id,name,salary){  
  3. this.id=id;  
  4. this.name=name;  
  5. this.salary=salary;  
  6. }  
  7. e=new emp(103,"Vimal Jaiswal",30000);  
  8.   
  9. document.write(e.id+" "+e.name+" "+e.salary);  
  10. </script>  
Test it Now

Output of the above example

103 Vimal Jaiswal 30000

Defining method in JavaScript Object

We can define method in JavaScript object. But before defining method, we need to add property in the function with same name as method.
The example of defining method in object is given below.
  1. <script>  
  2. function emp(id,name,salary){  
  3. this.id=id;  
  4. this.name=name;  
  5. this.salary=salary;  
  6.   
  7. this.changeSalary=changeSalary;  
  8. function changeSalary(otherSalary){  
  9. this.salary=otherSalary;  
  10. }  
  11. }  
  12. e=new emp(103,"Sonoo Jaiswal",30000);  
  13. document.write(e.id+" "+e.name+" "+e.salary);  
  14. e.changeSalary(45000);  
  15. document.write("<br>"+e.id+" "+e.name+" "+e.salary);  
  16. </script>  
Test it Now

Output of the above example

103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000

No comments:

Post a Comment