var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}var Extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}
var Calendar = Class.create();
/*执行到这里时,Class对象里的create方法,里面还没有 initialize属性啊
要执行完下面Calendar.prototype才有啊,这里为什么可以用
或者说
执行Class.create();时,create()方法里的this是指Class,而此时的Class还没有initialize
*/
Calendar.prototype = {
  initialize: function(container, options) {
    this.Container = $(container);//容器(table结构)
    this.Days = [];//日期对象列表
    
    this.SetOptions(options);
    
    this.Year = this.options.Year || new Date().getFullYear();
    this.Month = this.options.Month || new Date().getMonth() + 1;
    this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
    this.onSelectDay = this.options.onSelectDay;
    this.onToday = this.options.onToday;
    this.onFinish = this.options.onFinish;    
    
    this.Draw();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
        Year:            0,//显示年
        Month:            0,//显示月
        SelectDay:        null,//选择日期
        onSelectDay:    function(){},//在选择日期触发
        onToday:        function(){},//在当天日期触发
        onFinish:        function(){}//日历画完后触发
    };
    Extend(this.options, options || {});
  },
  //当前月
  NowMonth: function() {
    this.PreDraw(new Date());
  },
  //上一月
  PreMonth: function() {
    this.PreDraw(new Date(this.Year, this.Month - 2, 1));
  },
........上面格式没对
/*执行到这里时,Class对象里的create方法,里面还没有 initialize属性啊
要执行完下面Calendar.prototype才有啊,这里为什么可以用
或者说
执行Class.create();时,create()方法里的this是指Class,而此时的Class还没有initialize
*/

解决方案 »

  1.   

    其实就是这样
    var Calendar = function() { this.initialize.apply(this, arguments); }
      

  2.   

    谢谢cloudgamer 
    一说就明白了
    这贴就作为我看日历源码有什么问题,就向cloudgamer请教的平台
      

  3.   

    这是另外一个问题:
    日期联动选择器:var oThis = this; //
    //日期改变事件
    addEventHandler(this.SelYear, "change", function(){
    oThis.Year = oThis.SelYear.value; oThis.SetDay(); oThis.onChange();
                     //这是为什么要赋值给oThis,直接用this不是一样吗
    });
      

  4.   

    奇怪,程序可以,但会报错
    this.SelMonth is undefined
    (?)()()DateSelector.htm (行 73)
    [Break on this error] this.Month = this.SelMonth.value; this.SetDay(); this.onChange();
    DateSelector.htm (行 73)
    this.SelYear is undefined
    [Break on this error] this.Year = this.SelYear.value; this.SetDay(); this.onChange();oThis和this都引用同一对象,为什么会这样