Skip to content

Instantly share code, notes, and snippets.

@sir-ragna
Created September 28, 2015 20:47

Revisions

  1. sir-ragna created this gist Sep 28, 2015.
    37 changes: 37 additions & 0 deletions wrap.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /* A function that give the first argument to the callback function */
    function wrapper(func) {
    return function(arg1, callback) {
    func(arg1, function() {
    callback(arg1);
    });
    };
    }

    function bar(arg1) {
    console.log("Bar: " + arg1);
    };
    function foo(arg1, callback) {
    console.log("Foo: " + arg1);
    callback();
    };

    // Test the wrapper
    var fooImproved = wrapper(foo);
    fooImproved("Hello", bar);

    /* Now lets add logging for all arguments */
    function addArgLogging(func) {
    return function() {
    for(var i = 0; i < arguments.length; i++) {
    // If your arguments contains more then strings
    // Say functions then you should add a check for that
    // here
    console.info("ARG " + i + ": " + arguments[i]);
    }
    func.apply(this, arguments)
    };
    }

    // Test the Arg logger
    var barLogged = addArgLogging(bar);
    barLogged("Test", "Hello", "World", "Hey!");