
Aspect Oriented Programming for Javascript. It allows you to change the behavior of, or add behavior to methods and functions (including constructors) non-invasively.
As a simple example, instead of changing code, you can use meld to log the result of
myObject.doSomething
:var myObject = {
doSomething: function(a, b) {
return a + b;
}
};
// Call a function after myObject.doSomething returns
var remover = meld.after(myObject, 'doSomething', function(result) {
console.log('myObject.doSomething returned: ' + result);
});
myObject.doSomething(1, 2); // Logs: "myObject.doSomething returned: 3"
remover.remove();
myObject.doSomething(1, 2); // Nothing logged
Docs
Quick Start
AMD
- Get it using one of the following
1. `yeoman install meld`, or
1. `bower install meld`, or
1. `git clone https://github.com/cujojs/meld`, or
1. `git submodule add https://github.com/cujojs/meld`
- Configure your loader with a package:
```js
packages: [
{ name: 'meld', location: 'path/to/meld', main: 'meld' },
// ... other packages ...
]
```
define(['meld', ...], function(meld, ...) { ... });
orrequire(['meld', ...], function(meld, ...) { ... });
Node
npm install meld
var meld = require('meld');
RingoJS
ringo-admin install cujojs/meld
var meld = require('meld');
Running the Unit Tests
Install buster.jsnpm install -g buster
Run unit tests in Node:
buster test
What's New
1.3.0
meld()
is now a function that adds aspects.
* **DEPRECATED:** `meld.add()`. Use `meld()` instead.
1.2.2
- Remove stray
console.log
.
1.2.1
- Fix for IE8-specific issue with meld's internal use of
Object.defineProperty
. - Internally shim Object.create if not available to so that meld no longer requires an Object.create shim to advise constructors in pre-ES5 environments.
1.2.0
meld.joinpoint()
- Access the current joinpoint from any advice type.- Bundled aspects:
* trace: trace method call entry/return/throw
* memoize: simple memoization for methods and functions
* cache: configurable caching aspect to do more than simple memoization
1.1.0
- Advice can be applied directly to methods on a function.
- Removed undocumented behavior that implicitly adds constructor prototype advice: to advise a prototype, pass the prototype as the advice target.
1.0.0
- Removed browser global -
window.meld
is no longer supported. See this post on the cujo.js Google Group for an explanation. - No functional change beyond browser global removal.
See the full Changelog here
References
- AspectJ and Spring Framework AOP for inspiration and great docs
- Implementation ideas from @phiggins42's uber.js AOP
- API ideas from jquery-aop