Skip to content

Instantly share code, notes, and snippets.

@brianium
Created March 21, 2012 18:05
Show Gist options
  • Save brianium/2150452 to your computer and use it in GitHub Desktop.
Save brianium/2150452 to your computer and use it in GitHub Desktop.
JavaScript AOP proof of concept
//AOP powers rely on invoke() being used instead of ()
Function.prototype.invoke = function(args) {
if (this.before) {
this.before(args);
}
val = this(args);
if (this.after) {
this.after(args);
}
return val;
}
Function.prototype.before = function(fn) {
this.before = fn;
}
Function.prototype.after = function(fn) {
this.after = fn;
}
Math.sin.before(function(arg){
console.log('before with ' + arg.toString());
});
Math.sin.after(function(arg){
console.log('after with ' + arg.toString());
});
Math.sin.invoke(1)
//before with 1
//after with 1
//0.8414709848078965
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment