lionheart 发表于 2013-2-5 00:25:39

mootools Class 继承机制

var Class=function(B){var A=function(){    // 存在initialize函数的话就执行此函数,否则直接返回    // arguments 在什么情况下会 === null ?    return(arguments!==null&&this.initialize&&$type(this.initialize)=="function")?this.initialize.apply(this,arguments):this;};$extend(A,this);A.prototype=B;A.constructor=Class;return A;};Class.prototype={extend:function(B){    var C=new this(null);    // 属性copy,如果原Class中存在此属性时,根据属性的type    // 对属性值进行合并或者包装;否则直接设置此属性值    for(var D in B)    {      var A=C;      C=Class.Merge(A,B);    }    // 构造新的Class    return new Class(C);}}Class.Merge=function(C,D){// 如果父Class中存在此属性,且不是同一个对象if(C&&C!=D){    var B=$type(D);    if(B!=$type(C))    {      return D;    }    switch(B)    {      // 如果此属性值是函数类型的话,创建新的函数对象A,将其parent属性      // 设置为父Class中的同名函数对象,并返回A      case"function":var A=function()      {      // 执行此函数时,会先对当前执行对象(子对象)的parent属性设置为该函数对象的      // parent属性(函数对象C),也即父Class中的同名函数对象,然后再用      // 子对象的上下文来执行扩展时定义的该函数对象D。      // 这样设置以后,在D中要想访问父类中的同名函数对象时(相当于java中在子类中通过super访问父类中的方法),      // 使用this.parent.call(this)即可。      this.parent=arguments.callee.parent;      return D.apply(this,arguments);      };      A.parent=C;      return A;      # 如果是一般对象的话,进行属性合并后直接返回      case"object":return $merge(C,D);    }}return D;};
页: [1]
查看完整版本: mootools Class 继承机制