Archive. Date of access: 12 Jan 2022

marak 6c0748869e [dist] Bump to v4.0.0 7 년 전
examples 291e42e892 [dist] First commit 8 년 전
test 9a65160df8 [dist] Added tests / coverage / ReadMe 7 년 전
.gitignore 9a65160df8 [dist] Added tests / coverage / ReadMe 7 년 전
ReadMe.md 2a308a658d [docs] Updated ReadMe 7 년 전
index.js 578a26829a [api] [fix] Better behavior for double binding 7 년 전
package.json 6c0748869e [dist] Bump to v4.0.0 7 년 전

ReadMe.md

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 patterns for JavaScript

Example Usage

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! ... '
})