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

时间:2009-09-23 18:13:48

标签: javascript inheritance

基本上如何使用下面这个模式调用基本方法?

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 个答案:

答案 0 :(得分: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;
};

干杯。