DHTML Methods

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email
function squirrle(color)
{
this. color= color;
this. chattering= false;

T hen add a method to change the status of its chattering. function Chatter (toggle)
{
if (toggle== "on")

{ this. chattering = true;}

else { this.chattering = false; }
}

Now let us make this method as part of the object. That is include the method name as part of the object definition.
function squirrel(color)

{this. color= color;
this.chattering = false;
this.Chatter= Chatter;

}
The completed example, with an HTML document:

<html>
<head>
<title>This is my first JavaScript Program</title>
<SCRIPT language="javascript">
<!--
function chatter(toggle)
{
if{toggle== "on")

this.chattering = true;

document.writeln("Chattering, chattering..");
}
else

{ this.chattering = false;

document.writeln("No sound..")
}
}
function squirrel(color)
{
this.color = color;

this.chattering = false;

this.Chatter = Chatter;

document.writein("A JavaScript squirrel is born");
}
//-->
</SCRIPT>

</HEAD>

<BODY>

<SCRIPT LANGUAGE= "Javascript"> var bornsquirrel = new squirrel ("blue");

bornsquirrel.Chatter("on");

bornsquirrel.Chatter("off");

</SCRIPT>

</BODY>

</HTML>

When the browser interprets the script, it loads and interprets the function in order. Although it would be more logical to place the squirrel's method after the
squirrel definition, the browser would generate an error because the Chatter function has not been defined yet.

Domain Name Search
www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.