Insert scripts into an existing function?

javascript, jquery

Solution

Here's a demo of the following. Updated to use a closure and remove the need for a temporary variable.

//your cool with lol
var cool = {
    lol: function() {
        alert('lol');
    }
}

//let's have a closure that carries the original cool.lol
//and returns our new function with additional stuff

cool.lol = (function(temp) { //cool.lol is now the local temp
    return function(){       //return our new function carrying the old cool.lol
        temp.call(cool);     //execute the old cool.lol
        alert('bar');        //additional stuff
    }
}(cool.lol));                //pass in our original cool.lol

cool.lol();
cool.lol();​

Problem

This function is built into the page and I cannot modify the original .js file: ``` cool.lol = function () { // contents here } ``` Is there a way for me to append this function with some of my own scripts? Like this: ``` cool.lol = function () { // contents here // i would like to add my own stuff here!!! } ``` Or is there a way for me to detect that the function has been executed so I can run something after it?

Original source