Skip to content
Simple AOP module
JavaScript Makefile
Find file
Latest commit 01e6430 Naoyuki-Kanezawa add Makefile and jasmine.json
Failed to load latest commit information.
test add Makefile and jasmine.json
.gitignore
.npmignore initial commit
.travis.yml tests on node and phantomjs
LICENSE initial commit
Makefile add Makefile and jasmine.json
README.md add "How to test" to README
advice.js initial commit
bower.json initial commit
index.js initial commit
karma.conf.js initial commit
package.json tests on node and phantomjs

README.md

advice.js

Build Status

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');
});

Installation

Node

$ npm install advice.js

Bower

$ bower install advice

Documentation

advice.before(base, fn) : function

Return compose a new function which calls a fn before base returns.

Order: fn -> base

advice.before(obj, method, fn) : void

Redefine the obj.method which will be called a fn before the original obj.method is called.

Order: fn -> obj.method

advice.after(base, fn) : function

Return compose a new function which calls a fn after base returns.

Order: base -> fn

advice.after(obj, method, fn) : void

Redefine the obj.method which will be called a fn before obj.method is called.

Order: obj.method -> fn

advice.around(base, fn) : function

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'

advice.around(obj, method, fn) : void

Redefine the obj.method which around obj.method by fn.

Tests

npm test

License

MIT

Something went wrong with that request. Please try again.