Simple AOP library for both Node.js and browsers.
function base() {}
// compose a new function which calls a function after base returns
var fn = advice.after(base, function() {
console.log('base was called');
});
var obj = {
foo: function() {}
};
// redefine a method which calls a function after obj.foo returns
advice.after(obj, 'foo', function() {
console.log('obj.foo was called');
});$ npm install advice.js$ bower install adviceReturn compose a new function which calls a fn before base returns.
Order: fn -> base
Redefine the obj.method which will be called a fn before the original obj.method is called.
Order: fn -> obj.method
Return compose a new function which calls a fn after base returns.
Order: base -> fn
Redefine the obj.method which will be called a fn before obj.method is called.
Order: obj.method -> fn
Return compose a new function which around the base by fn.
function base() {}
var results = [];
var fn = advice.around(base, function(base, value) {
var result = 'around: ' + value;
// before
results.push('before: ' + value);
// base
base('base was called');
// after
results.push('after: ' + value);
return result;
});
fn('foo'); // 'around: foo'
results.join(', ');// 'before: foo, base: bar, after: foo'Redefine the obj.method which around obj.method by fn.
npm testMIT