/*  PublishSubcribe.js  ----------------  Author: Zach Perkitny  Version: 0.0.1  ----------------  Lightweight plugin implementation of the PubSub design pattern and a useful  DOM selector function to add event listeners.*/window.PublishSubcribe = (function(window, document, undefined){  //PubSub  var PublishSubcribe = PublishSubcribe || {};  //topics  var topics = {};  PublishSubcribe.subscribe = function(topic, listener){    //if topic doesn't exist, create it    if(!topics.hasOwnProperty(topic)) {      topics[topic] = [];    }    //add listener    if(listener && (typeof listener === 'function')){      //get index of listener      var i = topics[topic].push(listener) - 1;      return {        remove: function(){          topics[topic].splice(i, 1);        }      };    } else {      try {        throw new Error('PublishSubcribe: No Listener supplied or the one supplied is not a function.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }  }  PublishSubcribe.publish = function(topic, data){    if(!topics[topic] || topics[topic].length < 1) {      try {        throw new Error('PublishSubcribe: Topic doesn\'t exist or no listeners set on topic.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }    //send event to all listeners    topics[topic].forEach(function(listener){      listener(data || {});    });  }  PublishSubcribe.getDomNode = function(domElement){    this.domNode = document.querySelectorAll(domElement);    if(this.domNode.length === 0){      try {        throw new Error('PublishSubcribe: The supplied DOM Element does not exist.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }    return this;  };  PublishSubcribe.on = function(eventListener, callback){    var args = Array.from(arguments);    if(args.length < 2){      try {        throw new Error('PublishSubcribe: Not enough arguments supplied. Event Listener and callback are required.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }    if(typeof eventListener !== 'string'){      try {        throw new Error('PublishSubcribe: Event listener must be a string.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }    if(typeof callback !== 'function'){      try {        throw new Error('PublishSubcribe: Callback must be a function.');      } catch (e) {        console.error(e.name + ': ' + e.message);        return;      }    }    for(var i = 0; i < this.domNode.length; i++){      document.addEventListener(eventListener, callback);    }  }  return PublishSubcribe;})(window, document);This is just a very basic implementation of the PubSub design pattern, and I have a few questions:Am I missing anything important to the actual implementation, and is my method of finding errors efficient?Are there any errors I may be overlooking, and should be accounted for?Will my method of checking for DOM elements work on all browsers?This is how one might use this plugin://subscribe to topicvar sub = PublishSubcribe.subscribe('event', function(data){  for(var prop in data){    console.log(prop, ':', data[prop]);  }});//publish to a topicsPublishSubcribe.publish('event', {'data':'SomeData', 'moreData':'SomeMoreData'});//unsubscribe to topicsub.remove();//add event listeners to DOM elements (can be chained)PublishSubcribe.getDomNode('.element').on('click', function(){  //do something});
PubSub and DOM element selector plugin
javascript