Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Forked from angus-c/advice.js
Created June 11, 2012 13:46
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 amatiasq/2910159 to your computer and use it in GitHub Desktop.
Save amatiasq/2910159 to your computer and use it in GitHub Desktop.
simplest advice functional mixin
// usage
withAdvice.call(targetObject);
var withSomething = function() {
this.wrap('walk', function() {
// before
this.base();
// after
});
};
withSomething.call(targetObject);
// implementation
function withAdvice() {
this.base = function() { };
this.wrap = function(method, funct) {
var original = this[method];
if (typeof original !== 'function')
return this[method] = funct;
this[method] = function() {
var base = this.base;
this.base = original;
var result = funct.apply(this, arguments);
this.base = base;
return result;
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment