开发者社区> 问答> 正文

寻找一个 JavaScript 继承的 函数?

寻找一个 JavaScript 继承的 函数?

展开
收起
a123456678 2016-07-15 15:28:21 1885 0
2 条回答
写回答
取消 提交回答
  • class a {
      b() {
        console.log('111');
      }
    }
    
    
    class a1 extends a {
      c() {
        console.log('222');
      }
    }
    2019-07-17 19:57:05
    赞同 展开评论 打赏
  • /** 摘自ExtJs, 并作了一点修改
      * Extends one class with another class and optionally overrides members with the passed literal. This class
      * also adds the function "override()" to the class that can be used to override
      * members on an instance.
      */
     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;
         };
     }(),/**
      * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
      */
     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;
             }
         }
     },
    2019-07-17 19:57:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载