JSPM

hook.js

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q41733F
  • License MIT

add pre or post hooks to your methods

Package Exports

  • hook.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hook.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hook.js

Add pre or post hooks to your methods.

Install

npm install hook.js

How to use

var Hook = require('hook.js');

You can add hook feature to a CLASS:

function YourClass() {}
YourClass.prototype.func = function() {
    console.log('inner log');
};

// add hook feature
Hook.extend(YourClass.prototype);

Then you can hook func to a instance of YourClass:

var obj = new YourClass();

obj.pre('func', function() {
    console.log('pre log'); 
});

obj.post('func', function() {
    console.log('post log');
});

Now, func would print:

pre log
inner log
post log

Chaining & Cancellation

When add multi pre/post, normally we will do:

obj.pre(f1);
obj.pre(f2);

However, we can also write in a fast & beautiful way:

obj.pre(f1).pre(f2);

If you donot want invoke f2 for whatever reason, just return true in f1:

function f1() {
    if (your_condition) {
        // all later added `pre`s will not invoked
        return true;
    }
    // do your stuff
}

Contact

Email : isaymeorg@gmail.com