I needed a way of adding some extra functionality to some existing functions but without wanting to touch the original functions’ code, essentially joining functions together.

This is what joint.js does.

I ended up making two versions, one for joining more than 2 functions which has more of an overhead, and a simpler one for just joining only two functions.

First include the joint function in your script

function joint(a){var b;return b=a[a.length-1],a.pop(),a=a.length>1?joint(a):a[0],function(){b.apply(new a)}}

Then use it like this;

// My original function that I want to extend
function my_func() {
    console.log( "Original functionality." );

// Function with the new functionality
function new_func() {
    console.log( "New functionality." );

// Add the functionality of new_func to my_func by joining functions
my_func = joint([ my_func, new_func ]);

// Now run the original function with the new functionality

 > Original functionality.
 > New functionality.

You can download the source on my GitHub here: https://github.com/Dayjo/joint/ or take a look at it working on JSBin

Alternatively you can see this post on coderwall.com here; https://coderwall.com/p/j-cega

Leave a Reply