Archive. Date of access: 12 Jan 2022
marak 6c0748869e [dist] Bump to v4.0.0 | 7 jaren geleden | |
---|---|---|
examples | 8 jaren geleden | |
test | 7 jaren geleden | |
.gitignore | 7 jaren geleden | |
ReadMe.md | 7 jaren geleden | |
index.js | 7 jaren geleden | |
package.json | 7 jaren geleden |
Adds before
and after
hooks to any JavaScript protoype chain.
npm install --save prototype-hooks
.before()
hook for all existing prototype methods.after
hook for all existing prototype methodsvar 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! ... '
})