A simple AOP implementation for Node.
npm install aspectjsA simple AOP implementation for Node. It works either with standalone functions or with object methods. It can be used
on the client-side with Browserify too.
!Build Status

Example
``javascript
aspectjs.after(myGreatObject, "someMethod").add(myGreatAdvice, "someOtherMethod");
// Thereafter whenever myGreatObject.someMethod is called,
// myGreatAdvice.someOtherMethod will be called automatically afterward.
`
method,
which applies the advice setup in after(), before(), or around().$3
#### before(joinpoint, [methodname])
Will add advice before the specified join point, once
add() is called. The join point can be either a function
or an object followed by a method name. Returns the advice object.#### after(joinpoint, [methodname])
Will add the advice after the specified join point, once
add() is called.
The join point can be either a function or an object followed by a method name.
Returns the he advice object.
#### around(joinpoint, [methodname])
Adds the advice round the specified join point, once
add() is called.
The join point can be either a function or an object followed by a method name.
Returns the advice object.These method requires an advice function/method that takes an Invocation object. Within the advice body,
invocation.proceed() should be called where the joinpoint should occur. The advice is
applied only after
add() is called on the returned advice object.
`javascript// Example advice object for around advice
adviser = {adviseFunction: function(invocation){
// Some code goes here.
invocation.proceed(); // Calls the original function.
// Some more code goes here.
}};
`
$3
#### add([thisArg,] function|string)
Returns the new function that wraps the original function passed to before(), after(), or around().Usage
$3
Both the advice and joinpoints can be standalone functions: aspectjs.before(joinpoint).add(advice).Or the advice can be an object method, while the joinpoint is standalone:
aspectjs..after(joinpoint).add(advice, 'methodname')$3
Both the advice and joinpoint can be object methods: aspectjs.before(joinpointObj, 'methodname').add(adviceObj, 'methodname')
Examples
$3
` javascript
const before = require('aspectjs').before;
let addAdvice = require("aspectjs").addAdvice;let advised, adviser, result;
advised = {
add: function(increment){this.left += increment; },
id: 'test',
left: 32,
top: 43
};
adviser = {
override: function(increment){ advised.left = increment; }
};
before(advised, "add").add(adviser, "override");
advised.add(2); // Should equal 4.
`$3
`javascript
const around = require('aspectjs').around;
let advised, adviser, result;
advised = {
add: function(increment){
this.left += increment;
},
id: 'test',
left: 32,
top: 43
};// The advice function/method should take an Invocation object as input.
// Then invocation.proceed() should be called where the joinpoint occurs.
adviser = {
override: function(invocation){
advised.left += 5; // 37
invocation.proceed(); // 39
advised.left -= 19;
}
};
around(advised, "add").add(adviser, "override");
``