https://github.com/marak/prototype-hooks
add before and after hooks to any JavaScript prototype chain
https://github.com/marak/prototype-hooks
Last synced: about 1 year ago
JSON representation
add before and after hooks to any JavaScript prototype chain
- Host: GitHub
- URL: https://github.com/marak/prototype-hooks
- Owner: Marak
- Created: 2017-01-22T09:38:20.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-25T05:00:16.000Z (over 9 years ago)
- Last Synced: 2024-10-29T11:58:20.910Z (over 1 year ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 10
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
Awesome Lists containing this project
README
# prototype-hooks
Adds `before` and `after` hooks to any JavaScript protoype chain.
## Installation
```
npm install --save prototype-hooks
```
## Features
- Adds `.before()` hook for all existing prototype methods
- Adds `.after` hook for all existing prototype methods
- Quickly enables Apsect-oriented Programming [AOP](https://en.wikipedia.org/wiki/Aspect-oriented_programming) patterns for JavaScript
## Example Usage
```js
var hooks = require('protoype-hooks');
var Creature = function (opts) {
this.name = opts.name;
};
Creature.prototype.talk = function (data, cb) {
cb(null, this.name + ' says ' + data.text);
};
hooks(Creature);
var larry = new Creature({ name: "Larry" });
larry.before('talk', function(data, next) {
data.text = data.text + "!";
next(null, data);
});
larry.after('talk', function(text, next) {
text = text + ' ... ';
next(null, text);
});
larry.talk({ text: 'hi'}, function (err, result){
console.log(err, result);
// outputs: 'Larry says hi! ... '
})
```