Javascript 继承和重载的实现

extend : function(){
     // inline overrides
     var io = function(o){
         for(var m in o){
             this[m] = o[m];
         }
     };
     var oc = Object.prototype.constructor;

     return function(sb, sp, overrides){
         if(typeof sp == 'object'){
             overrides = sp;
             sp = sb;
             sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
         }
         var F = function(){}, sbp, spp = sp.prototype;
         F.prototype = spp;
         sbp = sb.prototype = new F();
         sbp.constructor=sb;
         sb.superclass=spp;
         if(spp.constructor == oc){
             spp.constructor=sp;
         }
         sb.override = function(o){
             Util.override(sb, o);
         };
         sbp.override = io;
         Util.override(sb, overrides);
         sb.extend = function(o){Util.extend(sb, o);};
         return sb;
     };
 }(),


 override : function(origclass, overrides){
     if(overrides){
         var p = origclass.prototype;
         for(var method in overrides){
             p[method] = overrides[method];
         }
         if(Util.isIE && overrides.toString != origclass.toString){
             p.toString = overrides.toString;
         }
     }
 },

发表评论

电子邮件地址不会被公开。 必填项已用*标注