Skip to content

Instantly share code, notes, and snippets.

@minwe
Last active July 14, 2020 06:41
Show Gist options
  • Select an option

  • Save minwe/133f9d80c62b6a41b60549f6e0e8d7fb to your computer and use it in GitHub Desktop.

Select an option

Save minwe/133f9d80c62b6a41b60549f6e0e8d7fb to your computer and use it in GitHub Desktop.
class PubSub {
constructor() {
this.handlers = [];
}
subscribe(event, handler, context) {
if (typeof context === 'undefined') {
context = handler;
}
this.handlers.push({event: event, handler: handler.bind(context)});
}
publish(event) {
for (let i = 0; i < this.handlers.length; i++) {
if (this.handlers[i].event === event) {
this.handlers[i].handler.call();
}
}
}
}
var PubSub = function() {
this.handlers = [];
};
PubSub.prototype = {
constructor: PubSub,
subscribe: function(event, handler, context) {
if (typeof context === 'undefined') {
context = handler;
}
this.handlers.push({event: event, handler: handler.bind(context)});
},
publish: function(event) {
for (var i = 0; i < this.handlers.length; i++) {
if (this.handlers[i].event === event) {
this.handlers[i].handler.call();
}
}
}
};
@AronDavis
Copy link
Copy Markdown

What about when you need to pass arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment