extendFunction.js =================


The easiest way to overwrite other functions with additional functionality

Example: Let's modify alert to keep a history array of the logs:

window.alertHistory = [];
extendFunction('alert', function(message) {

Test it:

alert('a message');
if (alertHistory[0] === 'a message') {
  alert('oh geez this function is powerful!');

Now let's add ' from DevinRhode2' to every alert message

extendFunction('alert', function(messagenativeAlert) {
  nativeAlert(message + ' from DevinRhode2')

Works for methods too:

extendFunction('console.log', function(message){
  //omg console.log was called! 

For non-global functions, you assign back like this:

localFunction = extendFunction(localFunction, function(arg1arg2originalLocalFunction){

Modify return values:

extendFunction('strangeModule.strangeMethod', function(optionsetc) {
  //convert arguments object into real array 
  var args = [].slice.call(arguments);
  //the last arg is the old strangeMethod. pop() that method off the args array and .apply with the proper args! 
  var returnValue = args.pop().apply(this, args);
  returnValue.extraInfo = 'idk';
  return returnValue;

MIT licensed