Skip to content

Instantly share code, notes, and snippets.

@anhnt
Forked from harrylove/gist:1230566
Created November 29, 2013 11:02
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 anhnt/7704265 to your computer and use it in GitHub Desktop.
Save anhnt/7704265 to your computer and use it in GitHub Desktop.
var truth = function() { return true; };
var relativeTruth = function() { return false; };
var assert = function(func) {
return func() == true;
};
var wrapper = function(func) {
// perform setup work here
// executed once when wrapper() is executed
console.info('the wrap is set');
// The result of calling wrapper() is to return an anonymous
// function that has wrapped the original function in a closure
return function() {
// perform wrapped work here
// executed every time the wrapped function is called
console.info('wrap, wrap, wrap, the boys are marching');
// now call the original method
var args = Array.prototype.slice.call(arguments)[0];
return func(args);
}
};
assert = wrapper(assert); // the wrap is set
console.info(assert(truth)); // 'wrap wrap wrap the boys are marching', true
console.info(assert(relativeTruth)) // 'wrap wrap wrap the boys are marching', false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment