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/

2

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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

function bothFuncs(){
    firstFunc();
    secondFunc();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.