HTML and CSS Reference
In-Depth Information
Figure 9.22
The number of days between two dates has been calculated.
with the
prototype
Property
All objects have a
prototype
property that allows you to extend the capabilities of the
object. With the
Date
object's
prototype
property, you can customize the time and the date
by providing new methods and properties that will be inherited by all instances of this
object. Because the
Date
object provides methods that return zero-based months, weeks,
years, and other measures, you might want to create a prototype method where
“January”
is month number 1 instead of 0, and the day is
“Monday”
instead of 1, and so on.
EXAMPLE
9.19
<html>
<head><title>The Prototype Property</title>
<script type = "text/javascript">
//
Customize the Date
1
function weekDay()
{
2
var now = this.getDay();
3
var names = new Array(7);
names[0]="Sunday";
names[1]="Monday";
names[2]="Tuesday";
names[3]="Wednesday";
names[4]="Thursday";
names[5]="Friday";
names[6]="Saturday";
4
return(names[now]);
}
5
Date.prototype.DayOfWeek=weekDay;
</script>
</head>
<body bgcolor="pink">
<font face="arial">
<big>
<div align="center">
<script type="text/javascript">
6
var today=new Date();
7
document.write("Today is " +
today.DayOfWeek()
+ ".<br />");
</script>
</div>
</big>
</font>
</body>
</html>