Last active
July 14, 2020 06:41
-
-
Save minwe/133f9d80c62b6a41b60549f6e0e8d7fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about when you need to pass arguments?