八. 綜合應用
最后一個例子演示JavaScript對象的重要性。首先設置好一個 Calendar(日歷)對象,然后根據需要顯示任何一個月的月歷。執行過程不復雜,只需要指定月和年為對象屬性,然后讓構造器做其它事情即可:
<script language="JavaScript"> /* Calendar object, calendar.js Usage: obj = new Calendar(mm, yyyy); created 15.Mar.2001
copyright Melonfire, 2001. all rights reserved. http://www.melonfire.com/community/columns/trog/
demonstration only - not meant for production enviroments!! */
// constructor function Calendar(month, year) {
// array of day names this.days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
// array of month names this.months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
// array of total days in each month this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// object properties - month and year // correction for zero-based array index this.month = month-1; this.year = year;
// leap year correction if (this.year % 4 == 0) { this.totalDays[1] = 29; }
// temporary variable - used later this.rowCount = 0;
// object method this.display = display;
// automatically run method display() once object is initialized this.display(); }
// function to display calendar function display() {
// create a Date object // required to obtain basic date information // get the first and last day of the month - boundary values for calendar obj = new Date(this.year, this.month, 1); this.firstDayOfMonth = obj.getDay(); obj.setDate(31); this.lastDayOfMonth = obj.getDay();
// start table document.write("<table border=0 cellpadding=2 cellspacing=5>");
// month display document.write("<tr><td colspan=7 align=center><font face=Arial size=-1><b>" + this.months[this.month] + " " + this.year + "</b></font></td></tr>");
// day names document.write("<tr>"); for (x=0; x<7; x++) {
document.write("<td><font face=Arial size=-2>" + this.days[x].substring(0,3) + "</font></td>") ; } document.write("</tr>");
// start displaying dates // display blank spaces until the first day of the month document.write("<tr>"); for (x=1; x<=this.firstDayOfMonth; x++) { // this comes in handy to find the end of each 7-day block this.rowCount++; document.write("<td><font face=Arial size=-2> </font></td>"); }
// counter to track the current date this.dayCount=1; while (this.dayCount <= this.totalDays[this.month]) { // use this to find out when the 7-day block is complete and display a new row if (this.rowCount % 7 == 0) { document.write("</tr>\n<tr>"); }
// print date document.write("<td align=center><font face=Arial size=-1>" + this.dayCount + "</font></td>"); this.dayCount++; this.rowCount++; } // end table document.write("</tr></table>"); }
// eof </script>
以下解釋一下上面代碼的工作過程:
最開始的幾行設置了包含月和日名的數組以及每個月中總的天數。用一個簡單的方程式來判斷某年是否為閏年,如果是的話就對二月的總天數進行相應修改。然后控制就傳給了對象方法display(),它負責將日歷寫到頁面上。
使用了Date對象和一些臨時變量之后,就創建了一個表格并用這個月的日期來填充好。document.write()方法負責設置 <table>、<tr> 和 <td> 標記,然后將日期信息打印到表格單元中。
以下代碼說明如何使用這個對象:
<html> <head> <script language="JavaScript" src="calendar.js"></script> </head>
<body bgcolor="white"> <script> obj1 = new Calendar(2, 2005); </script> <script> obj2 = new Calendar(7, 2001); </script> </body> </html>
代碼運行。
這就是全部了。我希望本文中的例子能讓你對 JavaScript對象的用途有一個正確的評價,也許還能為你在開發中使用它們提供幾個有用的思路。
|