Created
February 11, 2013 12:32
-
-
Save nanha/4754182 to your computer and use it in GitHub Desktop.
aop.js
This file contains hidden or 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
| var AOP = { | |
| addBefore : function(obj, fname, before) { | |
| var oldFunc = obj[fname]; | |
| obj[fname] = function() { | |
| before(true, arguments, obj); | |
| return oldFunc.apply(this,arguments); | |
| }; | |
| }, | |
| addAfter : function(obj, fname, after) { | |
| var oldFunc = obj[fname]; | |
| obj[fname] = function() { | |
| result = oldFunc.apply(this, arguments); | |
| try{ | |
| return result; | |
| } finally{ | |
| after(result, arguments, obj); | |
| } | |
| }; | |
| }, | |
| addAround : function(obj, fname, around) { | |
| var oldFunc = obj[fname]; | |
| obj[fname] = function(args) { | |
| return around(arguments, obj, | |
| function(){ | |
| return oldFunc.apply(this, obj[fname].arguments); | |
| } | |
| ); | |
| }; | |
| }, | |
| isPattern : function (re, funcName) { | |
| return (re.exec(funcName)!=null); | |
| }, | |
| addPattern : function(obj, pattern, oldfn, advisor){ | |
| var re = new RegExp(pattern); | |
| for(var o in obj){ | |
| if(typeof(obj[o])!="function") continue; | |
| if(this.isPattern(re, o)){ | |
| advisor.call(this, obj, o, oldfn); | |
| } | |
| } | |
| }, | |
| addBeforePattern : function(obj, pattern, before){ | |
| this.addPattern(obj, pattern, before, this.addBefore); | |
| }, | |
| addAfterPattern : function(obj, pattern, before){ | |
| this.addPattern(obj, pattern, before, this.addAfter); | |
| }, | |
| addAroundPattern : function(obj, pattern, before){ | |
| this.addPattern(obj, pattern, before, this.addAround); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment