Created
September 19, 2016 08:47
-
-
Save m3g4p0p/3a5d06c193d7740790c89adf0703c39f to your computer and use it in GitHub Desktop.
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
(function(global) { | |
global.AOP = function(subject) { | |
return { | |
after(method, advice) { | |
const original = subject[method]; | |
subject[method] = function() { | |
const result = original.apply(this, arguments); | |
return advice.call(this, result) || result; | |
} | |
return this; | |
}, | |
before(method, advice) { | |
const original = subject[method]; | |
subject[method] = function() { | |
const result = advice.apply(this, arguments); | |
return original.apply(this, (result || arguments)); | |
} | |
return this; | |
}, | |
error(method, advice) { | |
const original = subject[method]; | |
subject[method] = function() { | |
try { | |
return original.apply(this, arguments); | |
} catch (e) { | |
return advice.call(this, e); | |
} | |
} | |
} | |
}; | |
}; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment