7

Basically how do I call a base method using this patter below?

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;
};

Thanks.

Sergey
  • 3,214
  • 5
  • 34
  • 47

1 Answers1

7
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;
};

Cheers.

Anatoliy
  • 29,485
  • 5
  • 46
  • 45
  • Anatoliy, why do I need to use .apply method in this case? if I can simply call basemethod(somedata)? – Sergey Sep 23 '09 at 20:27
  • Apply will execute function "basemethod" in context of object "that", i.e. in the method "basemethod" you may use "this" - it will be links to that. If you use basemethod(somedata), function will running in global (window) context, which is incorrect in this case. Sample:
    window.myvar = 'window context var';
    object = {
        myvar: 'object context var'
    };
    var fun = function () {
        alert(this.myvar);
    }
    fun();
    fun.apply(object);
    
    – Anatoliy Sep 24 '09 at 05:49