Title: Toying with Javascript modules
Slug: toying-with-javascript-modules
Date: 2013-08-18 21:50:03
Author: Kartones
Lang: en
Tags: Development, Javascript, Patterns & Practices
Description: About structuring JavaScript code in modules, and an example basic pub-sub system for small projects.

 <p>I'm not a fan of rewriting things from scratch, but I also dislike bloating small things with lots of frameworks when they are not needed. </p> <p>Reading today about structuring javascript code in modules and how to communicate them I decided to make a small example to see how clean and small would be a very basic <a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">pub-sub</a> system communicating those modules (and avoid callback-hells at least for normal scenarios). Nothing fancy, nothing big, just the skeleton and most basic functionality, specially since I will probably just use it for small pet projects and experiments, where a framework is overkill.</p> <p>The code has no comments (but doesn't need them I think), I'm not using anything nice to do on-demand module loading, and basically there is no demo apart from <a href="https://github.com/Kartones/JSAssorted/blob/master/pubsub-modules/index.html">a single html file</a> doing some sample config, instantiations and message sending. </p> <p>Checking that modules being registered implement required methods (have certain functions, as there are no interfaces in JS) and a bit more of error handling would be nice too, I might add it when I start using the code in a real scenario and not this quick afternoon tests.</p> <p>So, taking into account it is nothing impressive, I like that it is pretty small, full sources counting 3 modules below 200 lines of code in total. Javascript looks ugly and messy at first, but can be "tamed" and then looks nicer.</p> <p>You can check all the code <a href="https://github.com/Kartones/JSAssorted/tree/master/pubsub-modules">at GitHub</a> if you wish.</p>
