Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 12, 2009 00:35
Show Gist options
  • Save isaacs/254637 to your computer and use it in GitHub Desktop.
Save isaacs/254637 to your computer and use it in GitHub Desktop.
function after (method, fn) {
var orig = this[method];
if (orig === after || fn === after) throw new Error(
"That's not a good idea. Please don't be dumb."
);
this[method] = function aop_after () {
var ret = orig.apply(this, arguments);
var newArgs = Array.prototype.slice.call(arguments,0);
if (ret !== undefined) newArgs.concat(ret);
var nextRet = fn.apply(this, newArgs);
return (nextRet !== undefined) ? nextRet : ret;
};
for (var i in orig) this[method][i] = orig[i];
return this;
}
function before (method, fn) {
var orig = this[method];
if (orig === before || fn === before) throw new Error(
"That's not a good idea. Please don't be dumb."
);
this[method] = function aop_before () {
var ret = fn.apply(this, arguments);
var newArgs = Array.prototype.slice.call(arguments,0);
if (ret !== undefined) newArgs.concat(ret);
var nextRet = orig.apply(this, newArgs);
return (nextRet !== undefined) ? nextRet : ret;
};
for (var i in orig) this[method][i] = orig[i];
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment