代码之家  ›  专栏  ›  技术社区  ›  Sergey

使用Douglas Crockford的函数继承调用Javascript中的基方法

  •  7
  • Sergey  · 技术社区  · 16 年前

    var GS = {};
    GS.baseClass = function (somedata) {
      var that = {};
      that.data = somedata;
    
      //Base class method
      that.someMethod = function(somedata) {
        alert(somedata);
      };
    
      return that;
    };
    
    GS.derivedClass = function(somedata) {
     var that = GS.baseClass(somedata);
    
     //Overwriting base method
     that.someMethod = function(somedata) {
       //How do I call base method from here? 
    
       //do something else
     };
     return that;
    };
    

    1 回复  |  直到 16 年前
        1
  •  7
  •   Anatoliy    16 年前
    var GS = {};
    GS.baseClass = function (somedata) {
      var that = {};
      that.data = somedata;
    
      //Base class method
      that.someMethod = function(somedata) {
        alert(somedata);
      };
    
      return that;
    };
    
    GS.derivedClass = function(somedata) {
     var that = GS.baseClass(somedata);
    
     //Overwriting base method
     var basemethod = that.someMethod;
     that.someMethod = function(somedata) {
       //How do I call base method from here? 
       basemethod.apply(that, [somedata]);
       //do something else
     };
     return that;
    };
    

    干杯。