Last active
June 18, 2018 20:31
-
-
Save leo60228/a49b32d03741dc5fa17f8375c7633b75 to your computer and use it in GitHub Desktop.
<0.3KB EventEmitter
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
"use strict";function EventEmitter(){var c=[];return{on:function(d,f){c.push({a:d,b:f})},emit:function(d){c.filter(function(f){return f.a==d}).forEach(function(f){return f.b()})},off:function(d){c.splice(c.indexOf(d),1)}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can save a lot of bytes when using arrow functions. As the cache (
c=[]
) is inside a block scope, you should uselet
and arrow functions for all public methods.I created a fork to show it: https://gist.github.com/cookiengineer/2c922967803e016c36b373d7fc0be634
The final result is 165 Bytes :)