new EventEmitter()
Create an event emitter class.
Example of usage:
// Class inherits the EventEmitter
class Foo extends EventEmitter
{
constructor(name) {
super();
this.name = name;
}
}
const foo = new Foo("John");
foo.addEventListener("change", (event) => {
console.log(event, this.name);
});
foo.emit("change");
// Use a mixin class
class Baz {
constructor(name) {
this.name = name;
}
sayHello() {
console.log(`hello ${this.name}`);
}
}
class Bar extends EventEmitter.mixin(Baz) {
sayByeBye() {
console.log(`Bye bye ${this.name}`);
}
}
const bar = new Bar("John");
bar.sayHello();
bar.sayByeBye();
bar.addEventListener("change", function (event) {
console.log(event);
});
bar.emit("change");
Dependencies:
- None
- Mixes In:
Methods
(static) mixin(Base) → {Class}
Mixin the Base object with the EventEmitter.
Parameters:
Name | Type | Description |
---|---|---|
Base |
Class | Base class to mix with |
Returns:
Mixin class
- Type
- Class
addEventListener(name, fn)
Register a new event.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | Name of the event. |
fn |
function | Function to trigger on event. |
emit(name, …args) → {portal.EventArg}
Event to emit.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | Event to emit. |
|
args |
* |
<repeatable> |
Arguments of the event. |
- Mixes In:
Returns:
Return the event argument.
- Type
- portal.EventArg
removeEventListener(name, fn)
Remove an event listener.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | Name of the event to remove. |
fn |
function | Function to remove from the listener. |