170

I have a function, a(), that I want to override, but also have the original a() be performed in an order depending on the context. For example, sometimes when I'm generating a page I'll want to override like this:

function a() {
    new_code();
    original_a();
}

and sometimes like this:

function a() {
    original_a();
    other_new_code();
}

How do I get that original_a() from within the over-riding a()? Is it even possible?

Please don't suggest alternatives to over-riding in this way, I know of many. I'm asking about this way specifically.

12 Answers 12

183

You could do something like this:

var a = (function() {
    var original_a = a;

    if (condition) {
        return function() {
            new_code();
            original_a();
        }
    } else {
        return function() {
            original_a();
            other_new_code();
        }
    }
})();

Declaring original_a inside an anonymous function keeps it from cluttering the global namespace, but it's available in the inner functions.

Like Nerdmaster mentioned in the comments, be sure to include the () at the end. You want to call the outer function and store the result (one of the two inner functions) in a, not store the outer function itself in a.

6
  • 27
    For any idiots out there like myself - pay close attention to the "()" at the end - without that, it returns the outer function, not the inner functions :)
    – Nerdmaster
    Commented Jul 24, 2012 at 18:27
  • 1
    @Nerdmaster Thanks for pointing that out. I added a note to hopefully help people notice. Commented Jul 24, 2012 at 20:11
  • 5
    Wow, I tried to do this without a namespace and I got an stack overflow, lol.
    – gosukiwi
    Commented Aug 10, 2012 at 17:00
  • i think the result would be the same if first and last parentheses are removed from function. Like from here (functi.. and }). Just doing }(); would produce the same result, as (function{})() 1st set of parentheses are used because you cant just declare, anonymous ,function expression you have to assign it. But by doing () you are not defining anything just returning a function. Commented Aug 4, 2013 at 12:09
  • 1
    @MuhammadUmer You're right that the parentheses around the function expression aren't necessary. I included them because without them, if you just look at the first couple lines, it looks (to me at least) like you're assigning the outer function directly to a, not calling it and storing the return value. The parentheses let me know there's something else going on. Commented Aug 5, 2013 at 12:59
93

The Proxy pattern might help you:

(function() {
    // log all calls to setArray
    var proxied = jQuery.fn.setArray;
    jQuery.fn.setArray = function() {
        console.log( this, arguments );
        return proxied.apply( this, arguments );
    };
})();

The above wraps its code in a function to hide the "proxied"-variable. It saves jQuery's setArray-method in a closure and overwrites it. The proxy then logs all calls to the method and delegates the call to the original. Using apply(this, arguments) guarantees that the caller won't be able to notice the difference between the original and the proxied method.

3
  • 11
    indeed. the usage of 'apply' and 'arguments' makes this far more robust than the other answers Commented May 21, 2012 at 19:15
  • Note that this pattern does not require jQuery—they're just using one of jQuery's functions as an example.
    – Kev
    Commented Apr 1, 2020 at 8:02
  • Using this on String.prototype.match() causes NodeJS to explode at the next ' typed.
    – Hoshi
    Commented Apr 13, 2022 at 15:27
28

Thanks guys the proxy pattern really helped.....Actually I wanted to call a global function foo.. In certain pages i need do to some checks. So I did the following.

//Saving the original func
var org_foo = window.foo;

//Assigning proxy fucnc
window.foo = function(args){
    //Performing checks
    if(checkCondition(args)){
        //Calling original funcs
        org_foo(args);
    }
};

Thnx this really helped me out

1
  • 3
    When following the proxy pattern, it will be important in some cases to implement this detail that is not present in this Answer's code: Instead of org_foo(args), call org_foo.call(this, args). That maintains this as it would have been when window.foo calls normally (not proxied). See this Answer
    – cellepo
    Commented Feb 20, 2016 at 2:30
11

You can override a function using a construct like:

function override(f, g) {
    return function() {
        return g(f);
    };
}

For example:

 a = override(a, function(original_a) {
      if (condition) { new_code(); original_a(); }
      else { original_a(); other_new_code(); }
 });

Edit: Fixed a typo.

3
  • +1 This is so much more readable than the other proxy pattern examples!
    – Mu Mind
    Commented May 26, 2011 at 17:42
  • 1
    ...but if I'm not mistaken, this is limited to zero-argument functions, or some predetermined number of arguments at least. Is there some way to generalize it to arbitrary numbers of arguments without losing the readability?
    – Mu Mind
    Commented May 26, 2011 at 17:48
  • @MuMind: yes, but using the proxy pattern properly - see my answer. Commented Mar 22, 2014 at 2:10
4

Passing arbitrary arguments:

a = override(a, function(original_a) {
    if (condition) { new_code(); original_a.apply(this, arguments) ; }
    else { original_a.apply(this, arguments); other_new_code(); }
});
1
  • 1
    wont arguments refer to original_a though?
    – Jonathan.
    Commented Apr 6, 2013 at 9:42
3

The answer that @Matthew Crumley provides is making use of the immediately invoked function expressions, to close the older 'a' function into the execution context of the returned function. I think this was the best answer, but personally, I would prefer passing the function 'a' as an argument to IIFE. I think it is more understandable.

   var a = (function(original_a) {
        if (condition) {
            return function() {
                new_code();
                original_a();
            }
        } else {
            return function() {
                original_a();
                other_new_code();
            }
        }
    })(a);
2

The examples above don't correctly apply this or pass arguments correctly to the function override. Underscore _.wrap() wraps existing functions, applies this and passes arguments correctly. See: http://underscorejs.org/#wrap

2

In my opinion the top answers are not readable/maintainable, and the other answers do not properly bind context. Here's a readable solution using ES6 syntax to solve both these problems.

const orginial = someObject.foo;
someObject.foo = function() {
  if (condition) orginial.bind(this)(...arguments);
};
1
  • But using ES6 syntax makes it rather restricted in use. Do you have a version which works in ES5?
    – eitch
    Commented Nov 30, 2016 at 11:06
0

I had some code written by someone else and wanted to add a line to a function which i could not find in the code. So as a workaround I wanted to override it.

None of the solutions worked for me though.

Here is what worked in my case:

if (typeof originalFunction === "undefined") {
    originalFunction = targetFunction;
    targetFunction = function(x, y) {
        //Your code
        originalFunction(a, b);
        //Your Code
    };  
}
0

I've created a small helper for a similar scenario because I often needed to override functions from several libraries. This helper accepts a "namespace" (the function container), the function name, and the overriding function. It will replace the original function in the referred namespace with the new one.

The new function accepts the original function as the first argument, and the original functions arguments as the rest. It will preserve the context everytime. It supports void and non-void functions as well.

function overrideFunction(namespace, baseFuncName, func) {
    var originalFn = namespace[baseFuncName];
    namespace[baseFuncName] = function () {
        return func.apply(this, [originalFn.bind(this)].concat(Array.prototype.slice.call(arguments, 0)));
    };
}

Usage for example with Bootstrap:

overrideFunction($.fn.popover.Constructor.prototype, 'leave', function(baseFn, obj) {
    // ... do stuff before base call
    baseFn(obj);
    // ... do stuff after base call
});

I didn't create any performance tests though. It can possibly add some unwanted overhead which can or cannot be a big deal, depending on scenarios.

0

So my answer ended up being a solution that allows me to use the _this variable pointing to the original object. I create a new instance of a "Square" however I hated the way the "Square" generated it's size. I thought it should follow my specific needs. However in order to do so I needed the square to have an updated "GetSize" function with the internals of that function calling other functions already existing in the square such as this.height, this.GetVolume(). But in order to do so I needed to do this without any crazy hacks. So here is my solution.

Some other Object initializer or helper function.

this.viewer = new Autodesk.Viewing.Private.GuiViewer3D(
  this.viewerContainer)
var viewer = this.viewer;
viewer.updateToolbarButtons =  this.updateToolbarButtons(viewer);

Function in the other object.

updateToolbarButtons = function(viewer) {
  var _viewer = viewer;
  return function(width, height){ 
blah blah black sheep I can refer to this.anything();
}
};
0

Not sure if it'll work in all circumstances, but in our case, we were trying to override the describe function in Jest so that we can parse the name and skip the whole describe block if it met some criteria.

Here's what worked for us:

function describe( name, callback ) {
  if ( name.includes( "skip" ) )
    return this.describe.skip( name, callback );
  else
    return this.describe( name, callback );
}

Two things that are critical here:

  1. We don't use an arrow function () =>.

    Arrow functions change the reference to this and we need that to be the file's this.

  2. The use of this.describe and this.describe.skip instead of just describe and describe.skip.

Again, not sure it's of value to anybody but we originally tried to get away with Matthew Crumley's excellent answer but needed to make our method a function and accept params in order to parse them in the conditional.

Not the answer you're looking for? Browse other questions tagged or ask your own question.