@swows/event-emitter
v1.0.0
Published
EventEmitter is an implementation of the Event-based architecture in JavaScript.
Downloads
1
Maintainers
Readme
@swows/event-emitter
通用JS事件派发器
Usage
引入该模块,会自动为Class引入3个事件扩展方法: addEventListener、removeEventListener 和 dispatchEvent
以及三个扩展方法对应的别名:on、off 和 emit
import Event from '@swows/event-emitter';
class MyClass extends Events {
constructor() {
super();
}
}
const eventsTest = new MyClass();
eventsTest.addEventListener('test1', () => {
// do something...
});
// alias to addEventListener
eventsTest.on('test2', () => {
// do something...
});
eventsTest.removeEventListener('test1', () => {
// do something...
});
// alias to removeEventListener
eventsTest.off('test2', () => {
// do something...
});
eventsTest.dispatchEvent('test1');
eventsTest.dispatchEvent('test1', {
// payload info
});
// alias to dispatchEvent
eventsTest.emit('test1');