Authour:I will teach you courses online
| C Language | C++ | Asp.net | VB.net | Oracle | Hardware and Networking | MCSE |
| Contact: | Name: Shrinivas | Mobileno:919703619391 | E-mail: vas.shrinivas002@gmail.com |
DOM objects and Built-in objects, We have all ready see about Date dom objects in previous chapters, If not click on below link.
Although JavaScript’s variables and arrays are most convenient to
store data. Sometimes you need big database. Like business card database, because
every business card contains names, phone numbers, Addresses.
With objects, you can make the
variables that store the db [database] as logical as business cards. Each
person identified by a Card object which has properties for name, address and
phone number.
How to Define an object:
You know that each object has certain methods and properties.
First we need to create object and then properties.
Object name is :Card
and properties are
>>NAME
>>ADDRESS
>>MOBILE PHONE
>>HOUSE PHONE
To genrate number of objects in javascript is to create a function to make new card objects. This function is called constructor in javascript
function Card(name,address,work,home){
this.name=name;
this.address=address;
this.workphone=work;
this.homephone=home;
}
Next print output like this.Let's create a function
function PrintCard() {
line1="Name: " + this.name + "<BR>";
line2="Address:" + this.address + "<BR>";
line3="Workphone:" + this.workphone + "<BR>";
line4="Homephone:" + this.homephone +"<BR>";
document.write(line1,line2,line3,line4);
}
line1="Name: " + this.name + "<BR>";
line2="Address:" + this.address + "<BR>";
line3="Workphone:" + this.workphone + "<BR>";
line4="Homephone:" + this.homephone +"<BR>";
document.write(line1,line2,line3,line4);
}
Above function will read the properties from current object by THIS and assign to
line1,line2,line3,line4
output with document.write(line1,line2,line3,line4)
Creating an Object Instance:
shrinivas=new Card("Vas shrinivas","new Avenue street","80987000000000","090000000" );
Now Card function is called, Assigned these parameters in to variables of Card function. And you created shrinivas object.
You did everything at a time, Above code you can write like this
1).Line-------------------------->shrinivas=new Card();
2).Line--------------------------->shrinivas.
Now if you write shrinivas. After you put (DOT after shrinivas) you will see all properties of Card function, they are name, address, work, home.
shrinivas=new Card();
shrinivas.name="Vas.Shrinivas";
shrinivas.address="new Avenue street";
shrinivas.work="80987000000000";
shrinivas.home="090000000";
Now this time to print everything
shrinivas.PrintCard();
Extending Built-in Objects
It's like extend power of objects, Means adding new features to
it.
How?
For
example, if u thinks the String object doesn't fit for our needs, we can
extend it.
title="Shrinivas welcomes you";
document.write(title.heading(1));
Example:
<html>
<head><title>Test of headings </title>
</head>
<body>
<script type="text/javascript">
function addhead (level) {
html="H" + level;
text=this.toString();
start="<" + html + ">";
stop="</" + html + ">";
return start + text + stop;
}
String.prototype.heading=addhead;
document.write("This is a heading 1" .heading(1));
document.write("This is a heading 2" .heading(2));
document.write("This is a heading 3" .heading(3));
</script>
</body>
</html>
No comments:
Post a Comment