Skip to content

Instantly share code, notes, and snippets.

@sir-ragna
Created September 28, 2015 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sir-ragna/d1c2562c7149fa98342a to your computer and use it in GitHub Desktop.
Save sir-ragna/d1c2562c7149fa98342a to your computer and use it in GitHub Desktop.
log args
/* 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!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment