putil-callable
v1.1.2
Published
A callable class implementation
Downloads
1,197
Readme
putil-callable
This package contains two very handy callable class implementations. Callable: Base extensible callable class CallableEventEmitter: Mixin of Callable and EventEmitter classes.
Installation
$ npm install putil-callable --save
Usage
Extend Callable in ES6
const {Callable} = require('putil-callable');
class MyCallable extends Callable {
constructor() {
super('handle');
}
handle(msg) {
console.log('MyCallable:', msg);
}
}
const obj1 = new MyCallable();
obj1('This is a callable class');
Extend Callable in ES5
const Callable = require('putil-callable').Callable;
function MyCallable() {
return Callable.call(this, 'handle');
}
MyCallable.prototype = Object.create(Callable.prototype);
MyCallable.prototype.constructor = MyCallable;
MyCallable.prototype.handle = function(msg) {
console.log('MyCallable:', msg);
};
const obj1 = new MyCallable();
obj1('This is a callable class');
Extend CallableEventEmitter in ES6
const {CallableEventEmitter} = require('putil-callable');
class MyCallable extends CallableEventEmitter {
constructor() {
super();
}
__call__(msg) {
this.emit('msg', msg);
}
}
const obj2 = new MyCallable();
obj2.on('msg', function(msg) {
console.log('CallableEventEmitter:', msg);
});
Node Compatibility
- node
>= 1
;