JavaScript Tutorial

Objects In JavaScript
Previous Home Next

JavaScript is not really an object oriented language, it is a prototyping language An object is a collection of properties

There are defferent way to use object in javascript .

  • using object literal
  • using instance of Object i.e new keyword
  • using an object constructor i.e using this keyword

using object literal

Syntax :

object={method1:value1,method2:value2.....methodN:valueN}  

Example :

<html>
<body>
<script>  
emp={id:"A-38 ",name:"rajesh Kumar",salary:50000}  
document.write(emp.id+" "+emp.name+" "+emp.salary);  
</script>
</body>
</html>

Output :

A-38 rajesh Kumar 50000

 

using instance of Object

Here new keyword is used to create an object

Syntax :

 var objectname=new Object();  

Example :

<html>
<body>
<script>
var book = new Object();
book.id = 121;
book.name = "java";
book.rate = 1000;
document.write(book.id+" "+book.name+" "+book.rate);
</script>
</body>
</html>

Output :

121 java 1000

object constructor i.e using this keyword

Here, used this keyword to show the Object method .

Example :

<html>
<body>
<script>  
function book(id,name,rate)
{  
this.id=id;  
this.name=name;  
this.rate= rate;  
}
b=new book (121,"core java",500);  
document.write(b.id+" "+b.name+" "+b.rate);  
</script>  
</body>
</html>

Output :

121 core java 500

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: