Created
March 21, 2012 18:05
-
-
Save brianium/2150452 to your computer and use it in GitHub Desktop.
JavaScript AOP proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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