Code Monkey home page Code Monkey logo

extendfunction.js's Introduction

extendFunction.js

The easiest way to overwrite other functions with additional functionality

Example: Let's modify alert to keep a history array of all the messages we alert:

// initialize empty alertHistory array for the messages..
window.alertHistory = [];
// extend alert with additional functionality. 
extendFunction('alert', function(args) {
  // args is an array of the arguments alert was called with
  // therefore, args[0] is the alert message.
  // We'll push this onto the alertHistory array for reference later
  alertHistory.push(args[0]);
});
// Test it!
alert('a message');
console.log(alertHistory);
if (alertHistory[0] === 'a message') {
  console.warn('extendFunction worked!');
}

So extendFunction takes 2 parameters: extendFunction(theFunction, extraFunctionality)

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

extendFunction('alert', function(args, nativeAlert) {
  // the second argument here is the nativeAlert function
  // precisely, that's window.alert before it was modified
  nativeAlert(args[0] + ' from DevinRhode2')
  // because you called nativeAlert, extendFunction doesn't.
  // however, if you don't call it, then extendFunction will
  // If you don't want the original function called, then you should just overwrite the function:
  // window.alert = function alertOverride() { ... };
});

extendFunction also works for methods:

extendFunction('console.log', function(args, nativeConsoleLog) {
  //omg console.log was called!
});

But if your functions are not global like alert and console.log, then you need to do this:

localFunction = extendFunction(localFunction, function(args, originalLocalFunction) {
  //your magic here!
});
//without extendFunction, it would look like this:
var oldLocalFunction = localFunction;
localFunction = function(paramA, paramB) {
 // your magic here!
 var args = Array.prototype.slice.call(arguments);
 return oldLocalFunction.apply(this, args);
};

Modify return values:

extendFunction('strangeModule.strangeMethod', function(args, prevFunc) {
  var returnValue = prevFunc.apply(this, args);

  returnValue.extraInfo = 'idk';
  return returnValue;
});

Or promises:

extendFunction('$.ajax', function(args, prevFunc) {
  var stackOnSend = new Error().stack;

  //prevFunc is the original $.ajax
  //call that and store the value to return
  var returnValue = prevFunc.apply(this, args);
  returnValue.fail(function(){
    console.error('request failed:', arguments, 'stackOnSend:', stackOnSend);
  });
  return returnValue;
});

MIT licensed

extendfunction.js's People

Contributors

devinrhode2 avatar malchak avatar

Watchers

James Cloos avatar Yaron Sadka avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.