0

I have the following:

var firstFunc = function (settings){
    alert('sup');
};

var secondFunc = function (settings){
    alert('dude');
};

$.extend(firstFunc, secondFunc);

firstFunc();

I'm trying to get the last firstFunc() to return sup, then dude.

Context

The reason for this is I have an inheritance pattern implemented where the base object has some functionality overridden if defined by the deriving object:

   if (child.close){
      base.close = child.close;    
    }

   base.close = function() {
      // do stuff    
    }

The issue is, I always need the base object to "do stuff", I want to append the functionality of child.close to base.close. How can I achieve this?

Fiddle: http://jsfiddle.net/jEjxZ/1/

BLAZORLOVER
  • 1,971
  • 2
  • 17
  • 27

2 Answers2

0

$.extend is used for objects, not functions. Why not just make a 3rd function that calls the first two?

function bothFuncs(){
    firstFunc();
    secondFunc();
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Hard to answer without knowing the exact inheritance implementation, but something like this might help you:

// reference to child instance
var self = this;

// overwrite close method
this.close = (function() { 
    var baseClose = self.close;
    return function() {
        baseClose.call(self);
        alert('child close');
    };
})();

And later:

base.close();   // alert('base close');
child.close();  // alert('base close'); -> alert('child close');

The idea to create a wrapper method.

Demo: http://jsfiddle.net/jEjxZ/3/

dfsq
  • 191,768
  • 25
  • 236
  • 258