var user = {
name: 'Jack'
};
user.onEvent('myCustomEvent', function (data) {
console.log(data);
});
user.trigger('myCustomEvent', 'hello world');
class Emitter {
constructor() {
var delegate = document.createDocumentFragment();
[
'addEventListener',
'dispatchEvent',
'removeEventListener'
].forEach(f => {
if (f !== 'dispatchEvent') {
this[f] = (...xs) => delegate[f](...xs)
} else {
this[f] = (eventName, ...xs) => delegate[f](new Event(eventName), ...xs)
}
})
}
}
// sample class to use Emitter
class Example extends Emitter {}
// run it
var e = new Example()
e.addEventListener('something', event => console.log('Something is happened!'))
e.dispatchEvent('something')
Minimitter = function Minimitter() {
this.listeners = {};
};
Minimitter.prototype.on = function(name, callback) {
this.listeners[name] = this.listeners[name] || [];
this.listeners[name].push(callback);
};
Minimitter.prototype.off = function(name, callback) {
this.listeners[name] = this.listeners[name] || [];
var indexOfCallback = this.listeners[name].indexOf(callback);
if(~indexOfCallback) {
this.listeners[name].splice(indexOfCallback, 1);
}
};
Minimitter.prototype.emit = function(name, data) {
this.listeners[name].forEach(function(callback) {
try {
callback(data);
} catch(err) {
console.error('A callback has failed.');
}
});
};
var test = Object.create(new Minimitter);
test.on("user", function (name) {
console.log("Username:", name)
})
test.emit("user", "Bob")
class EventEmitter {
constructor() {
this.listeners = {};
}
on(event, cb) {
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push(cb);
}
emit(event) {
for (let cb of this.listeners[event]) {
cb();
}
}
}
class User extends EventEmitter {
constructor(name) {
super();
this.name = name;
}
}
const user = new User("Alex");
user.on("greeting", () => {
console.log("Hello " + user.name);
});
user.on("farewell", () => {
console.log("Bye bye " + user.name);
});
setTimeout(() => {
user.emit("greeting"); // Hello Alex
}, 3000);
setTimeout(() => {
user.emit("farewell"); // Bye bye Alex
}, 6000);