168

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.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Kev
  • 15,899
  • 15
  • 79
  • 112

12 Answers12

182

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.

Viccari
  • 9,029
  • 4
  • 43
  • 77
Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
  • 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 Jul 24 '12 at 18:27
  • 1
    @Nerdmaster Thanks for pointing that out. I added a note to hopefully help people notice. – Matthew Crumley Jul 24 '12 at 20:11
  • 5
    Wow, I tried to do this without a namespace and I got an stack overflow, lol. – gosukiwi Aug 10 '12 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. – Muhammad Umer Aug 04 '13 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. – Matthew Crumley Aug 05 '13 at 12:59
  • that's true during whatever i was doing, after coming back from train of my thoughts i would have to do mini review what is that i am doing in code and get hugely confused why this function is getting assigned to this, then after reading line by line i'd see and remember oh yea.. – Muhammad Umer Aug 05 '13 at 21:52
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.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
PhilHoy
  • 1,613
  • 1
  • 12
  • 20
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

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Naresh S
  • 765
  • 2
  • 10
  • 19
  • 3
    When following the [proxy pattern](http://api.jquery.com/Types/#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](http://stackoverflow.com/a/915618/1357094) – cellepo Feb 20 '16 at 02: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.

Community
  • 1
  • 1
Hai Phan
  • 111
  • 1
  • 3
  • +1 This is so much more readable than the other proxy pattern examples! – Mu Mind May 26 '11 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 May 26 '11 at 17:48
  • @MuMind: yes, but using the proxy pattern properly - see [my answer](http://stackoverflow.com/a/296713/1269037). – Dan Dascalescu Mar 22 '14 at 02: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(); }
});
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

tobylaroni
  • 843
  • 8
  • 16
nevf
  • 4,596
  • 6
  • 31
  • 32
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);
};
James L.
  • 12,893
  • 4
  • 49
  • 60
  • But using ES6 syntax makes it rather restricted in use. Do you have a version which works in ES5? – eitch Nov 30 '16 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
    };  
}
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Sagar Sodah
  • 112
  • 1
  • 6
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.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
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();
}
};
SteckDEV
  • 489
  • 4
  • 13
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.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245