Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tom32i/event-emitter.js
Minimalist JS Event Emitter system for native object to extend.
https://github.com/tom32i/event-emitter.js
es6 events javascript
Last synced: 21 days ago
JSON representation
Minimalist JS Event Emitter system for native object to extend.
- Host: GitHub
- URL: https://github.com/tom32i/event-emitter.js
- Owner: Tom32i
- License: mit
- Created: 2014-06-03T11:04:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-26T08:35:40.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T04:27:09.930Z (about 1 month ago)
- Topics: es6, events, javascript
- Language: JavaScript
- Homepage:
- Size: 303 KB
- Stars: 6
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tom32i-event-emitter.js
=======================Minimalist JS Event Emitter system
## Install:
npm install tom32i-event-emitter.js
### Import
HTML:
```html
```
ES modules:
```javascript
import EventEmitter from 'tom32i-event-emitter.js';
```CommonJs modules:
```javascript
const EventEmitter = require('tom32i-event-emitter.js');
```### Usage
Make your prototype extend the EventEmitter.
Emit event with the `emit` method.
``` javascript
/**
* Player
*/
class Player extends EventEmitter {
constructor() {
super();this.alive = true;
}/**
* Emit and event on death
*/
die() {
this.alive = false;// Emitting an event:
this.emit('die', { player: this, foo: 'bar' });
}
}
```Listening for events with `on` / `off`:
```javascript
var player = new Player();function onDie(event) {
const { type, detail } = event;
const { foo, player } = detail;
// ...
}// Adding a listener
player.on('die', onDie); // "on" is an alias of "addEventListener"// Removing a listener
player.off('die', onDie); // "off" is an alias of "removeEventListener"
```# Contribute
Clone the repository:
git clone [email protected]:Tom32i/event-emiter.js.git
Install dev dependencies:
npm install
### Launch the dev server
make start
Go to http://localhost:8080
### Code quality
Linting:
make lint
Run tests:
make test