Created
          July 29, 2015 02:54 
        
      - 
            
      
        
      
    Star
      
          
          (193)
      
  
You must be signed in to star a gist 
- 
              
      
        
      
    Fork
      
          
          (76)
      
  
You must be signed in to fork a gist 
- 
      
- 
        Save learncodeacademy/777349747d8382bfb722 to your computer and use it in GitHub Desktop. 
    Basic Javascript PubSub Pattern
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | //events - a super-basic Javascript (publish subscribe) pattern | |
| var events = { | |
| events: {}, | |
| on: function (eventName, fn) { | |
| this.events[eventName] = this.events[eventName] || []; | |
| this.events[eventName].push(fn); | |
| }, | |
| off: function(eventName, fn) { | |
| if (this.events[eventName]) { | |
| for (var i = 0; i < this.events[eventName].length; i++) { | |
| if (this.events[eventName][i] === fn) { | |
| this.events[eventName].splice(i, 1); | |
| break; | |
| } | |
| }; | |
| } | |
| }, | |
| emit: function (eventName, data) { | |
| if (this.events[eventName]) { | |
| this.events[eventName].forEach(function(fn) { | |
| fn(data); | |
| }); | |
| } | |
| } | |
| }; | 
var events = (function() { var events = {}; function on(eventName, fn) { events[eventName] = events[eventName] || []; events[eventName].push(fn); } function off(eventName, fn) { if (events[eventName]) { for (var i = 0; i < events[eventName].length; i++) { if( events[eventName][i] === fn ) { events[eventName].splice(i, 1); break; } } } } function emit(eventName, data) { if (events[eventName]) { events[eventName].forEach(function(fn) { fn(data); }); } } return { on: on, off: off, emit: emit }; })();
That’s iife
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Skhmt try
events: Object.create( null )