Creating Objects
By: aathishankaran Printer Friendly Format
Most of the time you will be referencing objects, which are built-in to the DOM.
However, you may want to create your own objects for storing
data within a JavaScript program. There are several ways to create a new
object, but we'll look at two: creating a direct instance of an object and
creating an object prototype.
direct instance of an
object
Despite the awkward sound name, a
"direct instance of an object" simply means creating a new single
object, such as myPetDog:
myPetDog=new Object () ;
myPetDog.name="Barney";
myPetDog.breed="beagle";
mypetDog.year=1981;
Assigning a method to your new
object is also simple. Assume that you already have coded a function named
woof0, which causes a barking sound to play:
myPetDog.woof=woof;
Prototype of an
object
Sometimes, you'll want to create a
"template" or prototype of an object. This does not create an actual
instance of the object, but defines the structure of the object. In the future,
then, you can quickly stamp out a particular instance of the object. Suppose
that instead of myPetDog, you created a prototype object named petDog. This
object could then be a template for a particular pet dog object. First, create
a function which defines the petDog structure:
function petDog(name, breed, year)
{
this. name = name;
.this. breed = breed;
this. year = year;
}
Now that the petDog prototype has
been set, you can quickly create single instances of a new object based on the
petDog structure:
myPetDog=new petDog(“barney","beagle",1981);
yourPetDog=new petDog(“max","terrier",1990);
Example:
<Html>
<Head>
<Title>User Defined Objects</Title>
<Script>
function Print ()
{
document.write (“Employee Number: “+this.Eno);
document.write ("<br>") ;
document.write ("Employee Name: "+this. Ename) ;
document.write ("<br>");
document.write ("Employee Salary: “+this. Salary) ;
}
function Hra ()
{
document.write ("<br>") ;
document.write("HRA: "+(this.Salary*SO/lOO»;
document.write("<br>");
}
function Employee(Eno,Ename,Salary)
{
this.Eno=Eno;
this.Ename=Ename;
this.Salary=Salary;
this.Hra=Hra;
this.Print=Print;
}
</ Script>
</Head>
<Body bgcolor=#c0c0c0>
<Script>
var emp0l=new Employee(l00,"aathishankaranâ€,90000);
emp0l. Print ( ) ;
emp0l. Hra( ) ;
</ Script>
</Body>
</Html>
Output
Employee Number : 100
Employee Name : aathishankaran
Employee Salary : 90000
HRA : 45000
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java