Adding code to a javascript function programmatically

I'm attempting to customize an existing JS library without modifying the original JS code. This code loads in a few external JS files which I do have access to, and what I'd like to do is change one of the functions contained in the original file without copying and pasting the whole thing into the second JS file.
So for example, the off limits JS might have a function like this:

var someFunction = function(){
    alert("done");
}

I'd like to be able to somehow append or prepend some JS code into that function. The reason is primarily that in the original untouchable JS the function is pretty enormous and if that JS ever gets updated, the function I overwrite it with will be out of date.

I'm not entirely sure this is possible, but I figured I'd check.


If someFunction is globally available, then you can cache the function, create your own, and have yours call it.

So if this is the original...

someFunction = function() {
    alert("done");
}

You'd do this...

someFunction = (function() {
    var cached_function = someFunction;

    return function() {
        // your code

        var result = cached_function.apply(this, arguments); // use .apply() to call it

        // more of your code

        return result;
    };
})();

Here's the fiddle


Notice that I use .apply to call the cached function. This lets me retain the expected value of this, and pass whatever arguments were passed in as individual arguments irrespective of how many there were.


first store the actual function in a variable..

var oldFunction = someFunction;

then define your own:

someFunction = function(){
  // do something before
  oldFunction();
  // do something after
};

You can make a function that calls your code, and then calls the function.

var old_someFunction = someFunction;
someFunction = function(){
    alert('Hello');
    old_someFunction();
    alert('Goodbye');
}

I don't know if you can update the function, but depending on how it is referenced, you can make a new function in its place:

var the_old_function = someFunction;
someFunction = function () {
    /* ..My new code... */
    the_old_function();
    /* ..More of my new code.. */
}

Also. If you want to change local context you have to recreate function. For example:

var t = function() {
    var a = 1;
};

var z = function() {
    console.log(a);
};

Now

z() // => log: undefined

Then

var ts = t.toString(),
    zs = z.toString();

ts = ts.slice(ts.indexOf("{") + 1, ts.lastIndexOf("}"));
zs = zs.slice(zs.indexOf("{") + 1, zs.lastIndexOf("}"));

var z = new Function(ts + "\n" + zs);

And

z() // => log: 1

But this is just the simplest example. It will take still a lot of work to handle arguments, comments and return value. In addition, there are still many pitfalls.
toString | slice | indexOf | lastIndexOf | new Function