Skip to content

Instantly share code, notes, and snippets.

@m3g4p0p
Created September 19, 2016 08:47
Show Gist options
  • Save m3g4p0p/3a5d06c193d7740790c89adf0703c39f to your computer and use it in GitHub Desktop.
Save m3g4p0p/3a5d06c193d7740790c89adf0703c39f to your computer and use it in GitHub Desktop.
(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