在JavaScript中应用Object (3)

作者:简简单单 2008-01-18
  八. 综合应用
  最后一个例子演示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);

相关文章

精彩推荐