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

Usage no npm install needed!

<script type="module">
  import extendFunction from 'https://cdn.skypack.dev/extend-function';



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(message, nativeAlert) {
  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(arg1, arg2, originalLocalFunction){

Modify return values:

extendFunction('strangeModule.strangeMethod', function(options, etc) {
  //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