Skip to content

Instantly share code, notes, and snippets.

@fidel-karsto
Created August 22, 2013 11:53
Show Gist options
  • Save fidel-karsto/6306243 to your computer and use it in GitHub Desktop.
Save fidel-karsto/6306243 to your computer and use it in GitHub Desktop.
Some basic AOP style functions.
var before = function(fn, pre) {
return function() {
pre.apply(this, arguments);
return fn.apply(this, arguments);
};
},
after = function(fn, aft) {
return function() {
var result = fn.apply(this, arguments);
aft.apply(this, result);
return result;
};
},
around = function(fn, pre, aft) {
return function() {
var result;
pre.apply(this, arguments);
result = fn.apply(this, arguments);
aft.apply(this, result);
return result;
};
};
@fidel-karsto
Copy link
Author

example

console.log = around(console.log, function() {
    console.info("tadaaa a log message is coming");
}, function () {
    console.info("tadaaa a log message has been");
});

will result in

console.log("foo");
// "tadaaa a log message is coming"
// "foo"
// "tadaaa a log message has been"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment