Google News
logo
Javascript Objects
JavaScript is an Object Oriented Programming (OOP) language. JavaScript offers significant object oriented capabilities, has its own built-in objects, and offers you the option of creating your own custom objects. For example: car, bike, chair, pen, keyboard, monitor, mouse etc.

Object is a non-primitive data type in JavaScript. In JavaScript object is a standalone entity because there is no class in JavaScript.

There are 3 ways to create objects.

1. Object literal
2. Creating instance of Object
3. Using an object constructor 
Javascript Object literal :
The object literal is a simple way of creating an object using { } brackets. Use comma (,) to separate multiple key-value pairs.
var <object-name> = { key1: value1, key2: value2,... keyN: valueN};
Example :
<html>
<head>
<title>Javascript Object literal</title>
</head>
<body> 
 
 
<script type="text/javascript">  

  emp={id:9, name:"Ramana Reddy", salary:36000} 
 
  //Display Multiple Line
  document.write("Emp ID : " + emp.id + "<br />"); 
  document.write("Emp Name : " + emp.name + "<br />");
  document.write("Emp Salary: " + emp.salary + "<br /><br />");
 
 
  //Display Single Line
  document.write(emp.id+" "+emp.name+" "+emp.salary);
 
</script>
 
</body>
</html>
Output :

Emp ID : 9

Emp Name : Ramana Reddy

Emp Salary: 36000


9 Ramana Reddy 36000

Javascript Instance of Object :
Instance of Object the following example :
var objectname=new Object();
Example :
<html>
<head>
<title>Javascript Instance of Object</title>
</head>
<body> 
 
<script type="text/javascript">  

  var emp=new Object();  
  emp.id=27;  
  emp.name="V V Ramana Reddy";  
  emp.salary=900000;
 
  //Display Multiple Line
  document.write("Emp ID : " + emp.id + "<br />"); 
  document.write("Emp Name : " + emp.name + "<br />");
  document.write("Emp Salary: " + emp.salary + "<br /><br />");
 
  //Display Single Line
  document.write(emp.id+" "+emp.name+" "+emp.salary);  

</script> 
 
</body>
</html>
Output :

Emp ID : 27

Emp Name : V V Ramana Reddy

Emp Salary: 90000


27 V V Ramana Reddy 90000

Javascript Object constructor :
Create an object is with Object Constructor using new keyword. You can attach properties and methods using dot notation.
var objectname=new Object();
Example :
<html>
<head>
<title>Javascript Object constructor</title>
</head>
<body> 
 
<script type="text/javascript">  
 
  function emp(id,name,salary){  
  this.id=id;  
  this.name=name;  
  this.salary=salary;  
  }  
  my_emp=new emp(36,"V V R Reddy",180000);  
 
  //Display Multiple Line
  document.write("Emp ID : " + my_emp.id + "<br />"); 
  document.write("Emp Name : " + my_emp.name + "<br />");
  document.write("Emp Salary: " + my_emp.salary + "<br /><br />");
 
  //Display Single Line
  document.write(my_emp.id+" "+my_emp.name+" "+my_emp.salary);  
 
</script>  
 
</body>
</html>
Output :

Emp ID : 36

Emp Name : V V R Reddy

Emp Salary: 180000


36 V V R Reddy 180000

Javascript Object Methods :
Create an object is with Object Constructor using new keyword. You can attach properties and methods using dot notation.
var objectname=new Object();
Example :
<html>
<head>
<title>Javascript Object Methods</title>
 
<script type="text/javascript">

  // Define a function which will work as a method
  function addPrice(amount){
  this.price = amount; 
  }
 
  function book(book_name, author){
  this.book_name = book_name;
  this.author  = author;
  this.addPrice = addPrice; // Assign that method as property.
  }

</script>
     
</head>
 <body>
   
      <script type="text/javascript">
         var myBook = new book("JavaScript", "V V Raamana Reddy");
         myBook.addPrice(150);
         
         document.write("Book Name is : " + myBook.book_name + "<br>");
         document.write("Book author is : " + myBook.author + "<br>");
         document.write("Book price is : " + myBook.price + "<br>");
      </script>
      
</body>
</html>
Output :

Book Name is : JavaScript

Book author is : V V Raamana Reddy

Book price is : 150